Handling various exceptions of the same type in Java?

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.

+5
source share
3 answers

In most cases, the message is not related to the point of view of your code. It is just something that needs to be shown to the user or logged in. The only significant fact is that the connection is broken for any reason, and there are no different code paths that you can use depending on what message it was.

The only thing that is different from "socket closed", which indicates an encoding error.

EDIT Regarding your comments below:

  • Any IOException other than a SocketTimeoutException on the socket is fatal to the connection.
  • Invalid packages do not raise an IOException : an application-level problem that throws application-level exceptions or subclasses of IOException: for example, java.io.StreamCorruptedException.
  • There is no such thing as IOException: connection closed by remote host . If the peer closes the connection, this causes a stream-end condition, which manifests itself either as read() returning -1, readLine() returning zero, or readXXX() throwing an EOFException for any other X.
+2
source

I would suggest catching the exceptions in order, from the most specific to the smallest - so that you notice a chain break pattern when it reaches what you are looking for. This is the best I can think of:

 try { /// Something that can result in IOException or a SocketException catch (IOException e){ //Do something specific }catch (SocketExcpetion e){ }catch (Exception e) { //or some superclass of the above exceptions /// } 

Do not forget that you can also use several exceptions from different types with the | : catch (IOException|SocketException|

+1
source

The documentation ( http://docs.oracle.com/javase/7/docs/api/java/io/IOException.html ) contains a long list of direct subclasses. You can view them and check which ones you want to relate differently. Once you know this, you can use several catch blocks, first subclasses, and then the most common IOException:

 catch(SSLException se) { // do something } catch(HttpRetryException he) { // do something else } catch(IOException ioe) { // nop } 
+1
source

All Articles