When handling errors in Java, it is usually observed that superclasses are errors, such as
Exception, IOException, SocketException, etc.
However, how can you find the details of the exception? How do you distinguish a certain type of exception from others. For example, I am currently working on a small project using Netty.io that throws an IOException for each type of read / write error you can name. This makes sense, because ultimately these are I / O errors, but how would I handle them individually.
Examples of exceptions:
java.io.IOException: An existing connection was forcibly closed by the remote host java.io.IOException: Connection reset by peer java.io.IOException: Stream closed
The list just goes on, but how would you relate to this separately, one approach that I found looking around and looking very nasty looks like this.
try { // ... } catch (IOException e) { if(e.getMessage().contains("An existing connection was forcibly closed by the remote host")) { // Handle error } else //... }
It seems very tedious, and if you want, this is the best way to do it, correct . Over the past few hours, I have looked through quite a few error handling emails, and they all only talk about the big boys that are commonly used. IOException, Exception, SocketException, NullPointerException, and FileNotFoundException . Where I believe that SocketException and FileNotFoundException will be directly related to IOException , most likely a subclass, correct me if I am wrong.
In any case, how to handle these exceptions correctly and how do you determine exactly which exception you should handle? All I really can do is handle an IOException until something more accurate appears, but when developing applications it is always good to be able to handle each error uniquely.