How to Catch Multiple Java Exceptions in the Same Catch Clause

Can I catch multiple Java exceptions in the same catch clause?

This has been possible since Java 7. The syntax for a multi-catch block is:

try { 
...
} catch (IllegalArgumentException | SecurityException | IllegalAccessException |
NoSuchFieldException e) {
someCode();
}

Remember, though, that if all the exceptions belong to the same class hierarchy, you can simply catch that base exception type.

Also note that you cannot catch both ExceptionA and ExceptionB in the same block if ExceptionB is inherited, either directly or indirectly, from ExceptionA. The compiler will complain:

Alternatives in a multi-catch statement cannot be related by subclassing
Alternative ExceptionB is a subclass of alternative ExceptionA

The fix for this is to only include the ancestor exception in the exception list, as it will also catch exceptions of the descendant type.

How to catch two or more Exceptions in JAVA?

In Java SE 7 and later, you actually can catch multiple exceptions in the same catch block.

To do it, you write it this way:

         try{
// Your code here
} catch (ExampleException1 | ExampleException2 | ... | ExampleExceptionN e){
// Your handling code here
}

Besides that, you can use an extremely general catching exception like:

try{
// code here
} catch (Exception e){
// exception handling code here
}

But, that is a discouraged practice. ;)

Resources: https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html (Oracle Documentation).

Is it possible in Java to catch two exceptions in the same catch block?

Java 7 and later

Multiple-exception catches are supported, starting in Java 7.

The syntax is:

try {
// stuff
} catch (Exception1 | Exception2 ex) {
// Handle both exceptions
}

The static type of ex is the most specialized common supertype of the exceptions listed. There is a nice feature where if you rethrow ex in the catch, the compiler knows that only one of the listed exceptions can be thrown.



Java 6 and earlier

Prior to Java 7, there are ways to handle this problem, but they tend to be inelegant, and to have limitations.

Approach #1

try {
// stuff
} catch (Exception1 ex) {
handleException(ex);
} catch (Exception2 ex) {
handleException(ex);
}

public void handleException(SuperException ex) {
// handle exception here
}

This gets messy if the exception handler needs to access local variables declared before the try. And if the handler method needs to rethrow the exception (and it is checked) then you run into serious problems with the signature. Specifically, handleException has to be declared as throwing SuperException ... which potentially means you have to change the signature of the enclosing method, and so on.

Approach #2

try {
// stuff
} catch (SuperException ex) {
if (ex instanceof Exception1 || ex instanceof Exception2) {
// handle exception
} else {
throw ex;
}
}

Once again, we have a potential problem with signatures.

Approach #3

try {
// stuff
} catch (SuperException ex) {
if (ex instanceof Exception1 || ex instanceof Exception2) {
// handle exception
}
}

If you leave out the else part (e.g. because there are no other subtypes of SuperException at the moment) the code becomes more fragile. If the exception hierarchy is reorganized, this handler without an else may end up silently eating exceptions!

Can you use multiple catch with same exception

You can't do that. To catch a exception inside a catch block you should user another try/catch.

public function() {
try {
return output;
} catch (Exception e){
try {
return output1;
} catch (Exception e2) {
return output2;
}
}
}

Is it possible to catch multiple exceptions and perform different logic upon them without using multiple catch statements?

That doesn't make sense. When you use a multi catch, then you are implicitly saying: all of "these" exceptions should fall into the same bucket.

Of course, you can then do some instanceof if/else trees, but heck: the java way of doing that would be to have different catch statements for each one.

But, also of course, depending on context, it might be pragmatic to do something like

 catch(XException | YException | ZException  xyOrZ) {
log(xyOrZ);
handle(xyOrZ);

where handle() does some instanceof "switching".

Long story short: multi catch is a convenient way to enable an aspect (such as logging) that works for all exceptions. But it can get into your way regarding exception specific handling. You simply have to balance your requirements, and use that solution that your team finds to best fit your needs. To a certain degree, this is about style, and style questions are decided by the people working the code base.

Specific and same actions when catching multiple exceptions

Use the catch (ExceptionA | ExceptionB e) construct. Within the catch block, first do an instanceof check for e and handle the exception types separately. After this, have the common handling for both types. This way you can do everything in one catch block:

try {
// do something...
} catch (ExceptionA | ExceptionB e) {
if (e instanceof ExceptionA) {
// handling for ExceptionA
} else {
// handling for ExceptionB
}
// common handling for both exception types
}

Catching multiple exceptions in Java-8

The type of the expression

b ? new Excep1() : new Excep2()

is Exception, since that's the common supertype of Excep1 and Excep2.

However, you are not catching Exception, so the compiler complains about it.

If you catch Exception, it will pass compilation:

public int m2(boolean b) {
try {
throw b ? new Excep1() : new Excep2();
} catch (Exception e) {
return 0;
}
}

I tried to find the JLS entry that explains the type of conditional ternary expression in your example.

All I could find was that this particular expression is a 15.25.3. Reference Conditional Expression.

I'm not entirely sure if it counts as a poly expression or a standalone expression. I think it's standalone (since poly expressions involve an assignment context or an invocation context, and I don't think a throw statement counts as either of those).

For a standalone expression: "If the second and third operands have the same type (which may be the null type), then that is the type of the conditional expression."

In your case, the second and third operands have three common types - Object, Throwable and Exception - the type of the expression must be one of the latter two, since, "The Expression in a throw statement must either denote a variable or value of a reference type which is assignable (§5.2) to the type Throwable."

It appears that the compiler picks the most specific common type (Exception), and therefore a catch (Exception e) solves the compilation error.

I also tried to replace your two custom exceptions with two sub-classes of IOException, in which case catch (IOException e) solves the compilation error.



Related Topics



Leave a reply



Submit