Friday, 22 May 2015

Multiple catch blocks in Java exception handling

There might be a case when a code enclosed with in a try block throws more than one exception. To handle these types of situations, two or more catch clauses can be specified where each catch clause catches a different type of exception. When an exception is thrown, each of the catch statement is inspected in order, and the first one whose type matches that of the thrown exception is executed.
After one of the catch statement, with in the catch block, executes, the others are bypassed and execution continues after the try-catch block.

Notice that with Java 7 and later it is possible to catch multiple exceptions in one catch block, which eliminates the duplicated code.


In this program there is an array with only one element which is zero. From main method, while calling this method a parameter is passed which is used as an index of the array. First time 0 is sent which will mean divide by a[0]. Since the value at that index is 0 thus it will result in divide by 0 and exception will be thrown. Next time 2 is sent but array has only one element so trying to access a[2] will result in ArrayIndexOutOfBoundsException. Both of these exceptions will be caught by separate catch blocks.

public class MultipleCatchDemo {
    private void calculateValue(int i){
        int a[] = {0};
            int b = 7/a[i];
        }catch(ArithmeticException aExp){
        }catch(ArrayIndexOutOfBoundsException aiExp){

    public static void main(String[] args) {
        MultipleCatchDemo mcDemo = new MultipleCatchDemo();


java.lang.ArithmeticException: / by zero
 at org.netjs.examples.impl.MultipleCatchDemo.calculateValue(
 at org.netjs.examples.impl.MultipleCatchDemo.main(
java.lang.ArrayIndexOutOfBoundsException: 2
 at org.netjs.examples.impl.MultipleCatchDemo.calculateValue(
 at org.netjs.examples.impl.MultipleCatchDemo.main(

Restriction with Multiple catch blocks in Java

When multiple catch statements are used, it is important to follow the order where the exception sub type is caught before any of their super type. Which means a catch block that catches an exception subclass must come before the catch clause that catches an exception super class.

This is because a catch statement that uses a super class will catch exceptions of that type plus any of its subclasses. Thus, a catch block which catches an Exception subclass would never be reached if it comes after its super class. Note that in Java, unreachable code is an error so this situation will result in a compiler error.


In the same code as used above if one more catch clause is added, as the first one, which uses Exception as parameter, that will result in compiler error.

multiple catch blocks

That's all for this topic Multiple catch blocks in Java exception handling. If you have any doubt or any suggestions to make please drop a comment. Thanks!

Related Topics

  1. Best practices for exception handling in Java
  2. try-catch block
  3. Nested try statements
  4. Lambda expression and exception handling
  5. throws clause
  6. try-with-resources in Java 7
  7. Creating custom exception class

You may also like -

No comments:

Post a Comment