Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling different exceptions of the same type in Java?

When handling errors in Java it's common to see the superclasses being the errors that are caugh, such as

Exception, IOException, SocketException, etc.

However how do you go about finding the nitty-gritty details on the exception? How do you single a certain exception type out from the others. For instance, I'm currently working on a small project using Netty.io which throws an IOException for every type of read/write error you can name. This makes sense, because ultimately this is input/output errors, but how would I handle them individually.

Example 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 continues to go on, but how would you go about handling these seperately, one approach that I've found while looking around and seems really nasty is the following.

try { 
    // ...
} catch (IOException e) { 
    if(e.getMessage().contains("An existing connection was forcibly closed by the remote host")) {
        // Handle error
    } else //...
}

This seems very tedious and there's bound to be a better way to do this, a correct way if you will. I've looked through quite a bit of error handling writeups over the last few hours and they all only talk about the big boys that are used commonly. IOException, Exception, SocketException, NullPointerException, and FileNotFoundException. Where I believe SocketException and FileNotFoundException would be directly related to the IOException, more than likely a subclass, correct me if I'm wrong.

Anyway, what's the proper way to go about handling these exceptions and how do you figure out exactly what kind of exception you need to be handling? All I can really do is handle IOException until something more precise comes up, but when developing applications it's always good to be able to handle each error uniquely.

like image 624
Hobbyist Avatar asked Apr 07 '15 20:04

Hobbyist


People also ask

Can you have multiple exceptions in Java?

Java allows you to catch multiple type exceptions in a single catch block. It was introduced in Java 7 and helps to optimize code. You can use vertical bar (|) to separate multiple exceptions in catch block.

How do you handle multiple exceptions in a single catch?

If a catch block handles multiple exceptions, you can separate them using a pipe (|) and in this case, exception parameter (ex) is final, so you can't change it. The byte code generated by this feature is smaller and reduce code redundancy.

How multiple exceptions are handled?

By handling multiple exceptions, a program can respond to different exceptions without terminating it. In Python, try-except blocks can be used to catch and respond to one or multiple exceptions. In cases where a process raises more than one possible exception, they can all be handled using a single except clause.

Can you throw multiple exceptions in one throw statement?

It is not possible to throw numerous exceptions in Java. We can specify multiple exceptions, but only one of them will be thrown.


2 Answers

In most of these cases the message is irrelevant from the point of view of your code. It's just something to be shown to the user, or logged. The only salient fact is that the connection is broken, for whatever reason, and there aren't different code paths you can use depending on which message it was.

The only one that's different is 'socket closed', which indicates a coding bug.

EDIT Regarding your comments below:

  • Any IOException other than SocketTimeoutException on a socket is fatal to the connection.
  • Invalid packets don't cause IOException: that's an application-layer problem that throws application-layer exceptions, or subclasses of IOException: e.g., java.io.StreamCorruptedException.
  • There is no such thing as IOException: connection closed by remote host. If the peer closes the connection, that causes an end-of-stream condition, which manifests itself as either read() returning -1, readLine() returning null, or readXXX() throwing EOFException for any other X.
like image 77
user207421 Avatar answered Oct 26 '22 23:10

user207421


I would suggest catching the exceptions in order, from most specific to least - such that you will notice a circuit break pattern when the exception you are looking for is reached. This is the best I can come up with:

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
 ///
}

Don't forget that you can also catch multiple exceptions of different types using the | command: catch (IOException|SocketException|

like image 21
edenzik Avatar answered Oct 27 '22 00:10

edenzik