I need some help with understanding the IOException. I've reviewed a lot of information on the internet, and looked at the technical specifications at Oracle's Java website.
Am I correct in my understanding of the IOException class and all of it's sub-classes, that there are no associated "error messages" or "return code" values?
So if one wanted to issue some message and/or return code value, one would have to insert them with the IOException catch logic?
If the above is true, how would one separate the various IOException subclasses?
e.g. If the application detected an IOException, what sort of IOException is it? End-Of-File, File-Is-Closed, File-Not_found, File-In-Use, etc.
There are no "return code" values in exceptions (in general), but they do contain error messages. And you should handle them in catch
blocks, where you can specify the type of exception you want to handle. You can have several catch
blocks after a try
block, to handle different types of exceptions differently. The catch blocks will be invoked in the order specified, and the first one with a suitable parameter type will handle the exception. So you should catch the more specific exception types first, then the more general ones.
Simplistic example:
try {
...
throw new FileNotFoundException("This is an error message");
...
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
...
} catch (EOFException e) {
System.out.println("End of file reached: " + e.getMessage());
...
} catch (IOException e) { // catch all IOExceptions not handled by previous catch blocks
System.out.println("General I/O exception: " + e.getMessage());
e.printStackTrace();
...
}
As you see in the last catch block, exceptions store the stack trace of their origin, which can be printed. However, it is usually not a good idea to print such messages directly like here; in real production code, you usually want to log these messages using a logging framework, or display (suitable parts of) them on a UI.
There are no error codes, but very often there are messages. For example, they can contain file name or other details that help you identify what went wrong.
See subclasses of IOException for more ideas on what exceptions you can get.
To handle them, you can use different catch
phrases. Remember to go from more to less specific exceptions (if you catch IOException
in the first block, the more specific blocks such as FileNotFoundException
will never work). Sometimes you may want to catch them all with a single catch (IOException)
- if you don't need to handle subclasses in a different way.
try {
// ...
} catch (FileNotFoundException e) {
// ...
} catch (IOException e) {
// ...
}
You should think about what exceptions you want to handle in a specific way (not with a catch all block). After you have found your candidates you have to catch them according to their inheritance tree, the more specific first then the more general (from subclasses to superclasses). Beware that if your black list contains too many exceptions that have to be caught and they belong to only one try you should consider splitting that try block into smaller pieces.
You can determine what class an exception or any object is by using instanceof. However for exceptions you would use a catch block.
} catch(FileNotFoundException e) {
// file not found handling
} catch(EOFException e) {
// handle reaching the End-Of_File.
} catch(IOException e) {
// generic IOException handling for any other IOException.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With