What is a multi-catch block?

A single try block can have multiple catch blocks associated with it we can have different catch block for a different type of exception. A multi-catch block is useful when we have multiple exceptions and for each exception, we want to handle it separately.

Points to remember

  1. A particular catch block will only execute if an error occurs in the try block.
  2. Each catch block must have a different exception handler.
  3. A generic catch block can handle all the exceptions.
  4. Catch blocks should follow most specific to the general order.
  5. Only one catch block will execute at a time.

Syntax

try {

    // Body of try block.
    // Statements that may cause an exception

} catch (<exception_class_1> <object_name>) {

    // Handle exception type 1

} catch (<exception_class_2> <object_name>) {

    // Handle exception type 2

} catch (<exception_class_3> <object_name>) {

    // Handle exception type 3

}
.
.
.
 catch (<exception_class_N> <object_name>) {

    // Handle exception type N

}

Example 1

An example program to demonstrate the working of a multi-catch block.

public class MultiCatchExample1 {

    public static void main(String[] args) {

        try {

            int numA = 10;
            int numB = 0;

            // Divide 10 by 0
            System.out.println(numA / numB);

        } catch (NullPointerException e) {

            /*
             * If null pointer exception 
             * occurs this block will execute
             */
            System.out.println(e);

        } catch (ArithmeticException e) {

            /*
             * If arithmetic exception 
             * occurs this block will execute
             */
            System.out.println(e);

        } catch (IndexOutOfBoundsException e) {

            /*
             * If index out of bounds exception 
             * occurs this block will execute
             */
            System.out.println(e);
        }
    }
}

Example 2

This example program is similar to the above one, but here null pointer exception occurs so control jumps to NullPointerException block only.

public class MultiCatchExample2 {

    public static void main(String[] args) {

        try {

            String str = null;

            // It will raise null pointer exception
            System.out.println(str.length());

        } catch (NullPointerException e) {

            /*
             * If null pointer exception 
             * occurs this block will execute
             */
            System.out.println(e);

        } catch (ArithmeticException e) {

            /*
             * If arithmetic exception 
             * occurs this block will execute
             */
            System.out.println(e);

        } catch (IndexOutOfBoundsException e) {

            /*
             * If index out of bounds exception 
             * occurs this block will execute
             */
            System.out.println(e);
        }
    }
}

Example 3

In this example program, NumberFormatException is raised but we haven’t defined catch block for NumerFormatException so it will execute the most general or base exception class.

public class MultiCatchExample3 {

    public static void main(String[] args) {

        try {

            // It will raise number format exception
            int num = Integer.parseInt("hello");

        } catch (NullPointerException e) {

            /*
             * If null pointer exception 
             * occurs this block will execute
             */
            System.out.println(e);

        } catch (ArithmeticException e) {

            /*
             * If arithmetic exception 
             * occurs this block will execute
             */
            System.out.println(e);

        } catch (IndexOutOfBoundsException e) {

            /*
             * If index out of bounds exception 
             * occurs this block will execute
             */
            System.out.println(e);

        } catch (Exception e) {

            /*
             * A default exception block. 
             */
            System.out.println(e);
        }
    }
}

What is combined exception catch?

From Java 7 and later you can use a single catch block to handle multiple different types of exceptions. The combined exception catch block can reduce code complexity and duplication.

Syntax

try {

    // Body of the try block.
    // Statements that may cause an exception

} catch (<exception_class_1> | <exception_class_2> 
        | <exception_class_3> | <exception_class_N> <object_name>) {

    // Handle all different type of exceptions

}

The catch clause defines the types of exceptions that the try block can handle, and each exception type is separated with a vertical bar (|).

Example

The following program will throw NullPointerException because here we are trying to get the length of a string (str) which is null.

public class  MultiCatchExample4 {

    public static void main(String[] args) {

        try {

            String str = null;

            // If will raise null pointer exception
            System.out.println(str.length());

        } catch (NullPointerException | ArithmeticException | IndexOutOfBoundsException e) {

            /*
             * If any of NullPointerException, ArithmeticException or IndexOutOfBoundsException
             * occurs this block will execute
             */
            System.out.println(e);

        }
    }
}