Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In Java, I want to do something like this:

try {     ...      } catch (/* code to catch IllegalArgumentException, SecurityException,              IllegalAccessException, and NoSuchFieldException at the same time */) {    someCode(); } 

...instead of:

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

Is there any way to do this?

like image 458
froadie Avatar asked Aug 16 '10 18:08

froadie


People also ask

How do you catch multiple exceptions in one catch?

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.

Can you have multiple catches in try catch?

Yes, we can define one try block with multiple catch blocks in Java.

Can I use more than one try block to 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?

If your code throws more than one exception, you can choose if you want to: use a separate try block for each statement that could throw an exception or. use one try block for multiple statements that might throw multiple exceptions.


2 Answers

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.

like image 173
OscarRyz Avatar answered Oct 15 '22 06:10

OscarRyz


Not exactly before Java 7 but, I would do something like this:

Java 6 and before

try {   //..... } catch (Exception exc) {   if (exc instanceof IllegalArgumentException || exc instanceof SecurityException ||       exc instanceof IllegalAccessException || exc instanceof NoSuchFieldException ) {       someCode();    } else if (exc instanceof RuntimeException) {      throw (RuntimeException) exc;         } else {     throw new RuntimeException(exc);   }  } 



Java 7

try {   //..... } catch ( IllegalArgumentException | SecurityException |          IllegalAccessException |NoSuchFieldException exc) {   someCode(); } 
like image 28
user454322 Avatar answered Oct 15 '22 06:10

user454322