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.