Reputation: 841
I have just learned how to use exceptions, and I have a bit of confusion with RuntimeExceptions since these specifically are unchecked. So I have this code:
String path = "foo/bar/local/stuff.txt";
File file = new File(path);
Isn't this not a FileNotFoundException if that path isn't found? How do I specify that in my method? Also after this step I do:
File[] listFiles = file.listFiles();
Doesn't that also throw an exception if:
1. It's not a directory, it's a file.
2. It doesn't exist.
Sorry if this seems like newbie questions, I still don't really know how to use exceptions. Can someone please clarify?
Upvotes: 1
Views: 206
Reputation: 7326
Alright, I'm adding code, so I'm just gonna add a new answer. The listFiles method will return null if the file is not a directory. To throw your own exception, do this:
public void exampleMethod() throws Exception {
File[] files = someFile.listFiles();
if(files == null) {
throw new Exception("File is not a directory, and as such cannot list files");
//You can also create your own exception class (extends Exception), and explicitly throw that, rather than just a general Exception
return;
}
}
If you just want it to print the stuff, and not throw an exception, just do
new Exception("blah").printStackTrace();
Upvotes: 1
Reputation: 6043
First of all, according to the Documentation:
public File(String pathname)
Throws: NullPointerException
- If the pathname argument is null
So File file = new File(path);
does not throw a FileNotFoundException
.
1) It will not an exception if it's a file.
2) It will not thrown an exception if it doesn't exist.
Bottom line, the only exception that will be thrown is a NullPointerException
if the pathname is null.
On the other hand File.createNewFile()
can throw an IOException
or a SecurityException
. File.ListFiles()
can also throw a security exception.
Upvotes: 0
Reputation: 143856
Hmm, I don't think this is the approach I'm looking for. They currently don't throw any exceptions. But I know that the code would fail in these situations, so I'd like to know how to add exceptions to them so that I can deal with those situations appropriately.
It doesn't throw an exception. In the listFiles()
documentation, it says:
Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.
So if it's a file, or a file that doesn't exist, it returns a null.
But here is where a RuntimeException comes into play. If say in a method that you've written:
public void doStuffToAnExistingDirectory(String dirname)
{
// stuff
}
And the method is written such that it expects a directory name of an existing directory, and somewhere in that method you do this:
File file = new File(dirname);
File[] listFiles = file.listFiles();
int num = listFiles.length;
This will throw a NullPointerException
if dirname
doesn't denote an existing directory. This exception is Runtime, so you don't have to catch it or declare it in your doStuffToAnExistingDirectory()
method, and you can let it propagate up until something eventually catches it. This is essentially saying that the method wasn't written to handle an exception where the directory doesn't exist. So during the running of this program, if dirname
somehow ends up being a directory that doesn't exist, a Runtime exception gets thrown.
You could choose to handle this case in your code (as opposed to during runtime, when the program is running), and do a check to see if listFiles
is null or not, and then perhaps throw an exception of your own to let whatever is calling the doStuffToAnExistingDirectory()
method know it just gave you a directory that didn't exist.
Upvotes: 0
Reputation: 347184
File
is virtual concept. In Java a File
is allowed to not exist (that's why we have File.exists()
). This allows you to specify a "possible" location (for writing to for example).
It also allows to make checks to see if the required file exists or not, allowing the developer to make decisions (over write the file, not load the file, do something else on a lock file)
listFiles
is another beast. Arguably, it probably should throw some kind of IOException
if you attempt to list a "file" or the directory does not exist. It's been apart of the API since Java 1. This would suggest that the design paradigm at the time was different from the one we're use to know.
You can see this in the changes to the Properties
class, where save
has been deprecated in favor of store
as it throws an IOException
Java 7 has introduced a new API to help improve some of these short comings and introduce a better working model for file manipulation
Upvotes: 0
Reputation: 28687
The File(String) constructor cannot throw a FileNotFoundException, so you need not do anything special for the first half of your question.
File#listFiles() doesn't throw a FileNotFoundException either. It can throw a SecurityException if access is denied, but that's it. If the file isn't a directory or doesn't exist, the method simply returns null, so you should nullcheck it before using the listed files.
File[] files = file.listFiles();
if (files != null) {
// file exists and is a directory
// interact with files
}
As for actually using exceptions, you can add a throws
statement if you wish to notify the compiler that the method may throw the noted exception(s), and that your method does not handle occurrences where that exception is thrown.
public void someMethod() throws IOException, SecurityException { /*...*/ }
Upvotes: 0
Reputation: 1314
You can still declare what exceptions your method throws in the signature:
public void foo() throws FileNotFoundException
Upvotes: 0