Google+

239. Catching Multiple Exceptions is a single catch block








Instead of writing the a separate catch block for handling multiple exceptions in the try block code, we can handle all the exceptions in the single catch block. This feature was introduced from Java 7.

Example of using multiple catch blocks to handle multiple exceptions -

try
{
          int x = 9/0;  //Throws ArithmeticException
          int a[] = new int[2];
          a[5] = 8;   //Throws ArrayIndexOutOfBoundsException
}
catch(ArithmeticException e)
{
       System.out.println("Exception Details - "+e);
}
catch(ArrayIndexOutOfBoundsException e)
{
      System.out.println("Exception Details - "+e);
}

Instead of writing the multiple catch block for handling multiple exceptions in the try block code, we can use the following syntax to handle all the exceptions in a single catch block as shown below -

Syntax -

catch(ArithmeticException | ArrayIndexOutOfBoundsException e)
{
    //Code
}

Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, create a new Java Class 'CatchingMultipleExceptions.java' with main( ) method in the existing Java Project 'Project 47' as shown below -



2. Write a code which throws two exceptions inside the try block as show below -



3. Now create a single catch block which can handle the above two thrown exceptions as shown below -


4. View the displayed error as shown below -



This error is occurred, as the java feature (i.e. single catch block feature handling more than one exceptions) was introduced from Java 7. Even though we have installed a Java version more than 7, we are still getting this error, as in Eclipse IDE, by default the Java Compiler Compliance level is set below 1.7. In order to use the Java 7 features, the Java Compiler Compliance level need to be set to 1.7 and more. Lets change the project compiler compliance to 1.7 by following the below steps.

5. View the error and select 'Change project compliance and JRE to 1.7' option from the error message as shown below -


6. Observe that the error got resolved as shown below -


7. Write a statement after the catch block to check whether the exception is handled and the code after the catch block is executed as shown below -



8. Save & Run the Java Class 'CatchingMultipleExceptions.java' and observe that exception is handled and the statement after the catch block got executed and printed in the Eclipse IDE console as output as shown below -



Hence we can use a single catch block to handle the multiple exceptions in the try block.






Please comment below to feedback or ask questions.

Using programmatic method instead of exception handling will be explained in the next post.







2 comments:

Ambitious kalpana said...

Hi Arun ! In above program we are handling multiple exceptions through one catch block but in Actual we are just handling one exception and the second exception gets skipped as the control of the program jumps to catch block once it encounters an exception. Like in the above program it is evident from the output that when it encounters arithmetic exception it skips arrayIndexOutOfBoundException and jumps to catch block. Correct me if I'm wrong ! (We are just getting message of ArithemticException,ArrayIndexOutOfBoundException is totally ignored)

Ambitious kalpana said...

Arun waiting for your reply !