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?
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:
If you just want it to print the stuff, and not throw an exception, just do