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
catchblocks, where you can specify the type of exception you want to handle. You can have severalcatchblocks after atryblock, 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:
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.