How to handle multiple exceptions in Java

How to handle multiple exceptions in Java:

Let’s learn what are multiple exceptions and how to handle multiple exceptions in Java with different examples. For handling exceptions, we use try-catch blocks in Java. We can use a single catch block or we can use multiple catch blocks to handle the exceptions. Let’s learn how to do that with examples.

Handling multiple exceptions with different catch blocks:

This example will show you how to handle multiple exceptions with different catch blocks.

class Example {
    public static void main(String[] args) {
        try {
            String n = null;
            System.out.println(n.charAt(0));
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        }
    }
}

In this example program, we are trying to read the character at index 0 of the string n. There are two catch blocks to handle the following exceptions:

  • NullPointerException and
  • ArrayIndexOutOfBoundsException

If you run this program, it will execute the first exception block i.e. NullPointerException as the value of the string n is null.

Java example to handle multiple exceptions

If it will get an ArrayIndexOutOfBoundsException before a NullPointerException, it will execute the second catch block. For example:

class Example {
    public static void main(String[] args) {
        try {
            String arr[] = new String[4];
            String n = null;

            System.out.println(arr[10]);
            System.out.println(n.charAt(0));
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        }
    }
}

This code will run the second catch block.

Handling multiple exceptions with different catch blocks:

We can merge the two exceptions in the above example into one. We have to separate the exceptions with pipe symbol or |. Note that this option is available from Java SE 7.

class Example {
    public static void main(String[] args) {
        try {
            String arr[] = new String[4];
            String n = null;

            System.out.println(arr[10]);
            System.out.println(n.charAt(0));
        } catch (NullPointerException | ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        }
    }
}

It will throw ArrayIndexOutOfBoundsException because this exception is raised first.

Handling multiple exceptions with the Exception class:

Since the Exception class is the base class of all other classes, we can use this class in the catch block. We don’t have to add other classes with the Exception class as all other classes are sub-class of this class.

class Example {
    public static void main(String[] args) {
        try {
            String arr[] = new String[4];
            String n = null;

            System.out.println(arr[10]);
            System.out.println(n.charAt(0));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

It will print the following exception:

java.lang.ArrayIndexOutOfBoundsException: 10
	at Example.main(Example.java:9)

You might also like: