Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java7 multiple exception handling

I have been trying to find out answer to this question but did not get any satisfactory explanation. Here is some background:

Java 7 allows us to catch multiple exceptions in a single catch block provided those exceptions are from diffrent hierarchy. Eg:

try {
    // some code

} catch(SQLException | FileNotFoundException e) {
    e.printStackTrace();
} catch(Exception e) {
    e.printStackTrace();
}

But if exceptions are from the same hierarchy we must use multiple catch blocks like:

try {
    // some code
} catch(FileNotFoundException  e) {
    e.printStackTrace();
} catch(IOException e) {
    e.printStackTrace();
}

But if I try to write code like below compiler complains that "The exception FileNotFoundException is already caught by the alternative IOException"

try {
    // some code
} catch(FileNotFoundException | IOException  e) { // compiler error
    e.printStackTrace();
}

Now my question is: Why compiler reports an error in last case, can't it figure out that FileNotFoundException is special case of IOException? This would save code duplication when my exception handling logic is same.

like image 278
justAbit Avatar asked May 08 '15 06:05

justAbit


People also ask

How do you handle multiple exceptions in Java?

Java Catch Multiple Exceptions A try block can be followed by one or more catch blocks. Each catch block must contain a different exception handler. So, if you have to perform different tasks at the occurrence of different exceptions, use java multi-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.

Can one block handle multiple exceptions?

In Java SE 7 and later, a single catch block can handle more than one type of exception. This feature can reduce code duplication and lessen the temptation to catch an overly broad exception.

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.


1 Answers

Why compiler reports an error in last case, can't it figure out that FileNotFoundException is special case of IOException?

Because FileNotFoundException is a subclass of IOException. In other words, the "FileNotFoundException |" part is redundant.

The reason why the code below is ok...

} catch(FileNotFoundException  e) {
    ...
} catch(IOException e) {
    ...
}

...is because here the IOException clause matters: If a SocketException is thrown for instance, it will pass the by the FileNotFoundException part, and get caught in the IOException clause.

like image 114
aioobe Avatar answered Nov 05 '22 09:11

aioobe