Multiple Catch Exceptions

HOME

In the previous tutorial, I have explained about the control flow of try catch and finally blocks in Java. In this tutorial, I’ll explain multiple catch exceptions. Java supports multiple catch exceptions, that means a try block can have more than one catch handlers. All catch blocks must be ordered from most specific to most general, i.e. catch for ArithmeticException must come before catch for Exception.

Scenario 1 – In the below example, an array is defined of size 10 and we want to perform an invalid arithmetic operation on element 15 of the array. In this case, we have defined 3 different catch handlers to handle the exception – Arithmetic Exception, ArrayIndexOutOfBounds Exception and Parent Exception. The try block has thrown exception – Arithmetic as the Arithmetic operation is invalid (divide by 0).

public class MultipleCatchDemo1 {

	public static void main(String[] args) {
		try {
			int a[] = new int[10];
			a[15] = 30 / 0;
		
        } catch (ArithmeticException e) {
			System.out.println("Arithmetic Exception occurs");
		
        } catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("ArrayIndexOutOfBounds Exception occurs");
		
        } catch (Exception e) {
			System.out.println("Parent Exception occurs");
		
        }
		System.out.println("Rest of the program");
	}
}

Output
Arithmetic Exception occurs
Rest of the program

Scenario 2 – In the below example, I’m throwing an exception which is not handled by first catch exception handler.

public class MultipleCatchDemo2 {

	public static void main(String[] args) {

		try {
			int a[] = new int[10];
			System.out.println(a[15]);

		} catch (ArithmeticException e) {
			System.out.println("Arithmetic Exception occurs");

		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("ArrayIndexOutOfBounds Exception occurs");

		} catch (Exception e) {
			System.out.println("Parent Exception occurs");

		}
		System.out.println("Rest of the program");
	}
}

Output
ArrayIndexOutOfBounds Exception occurs
Rest of the program

Scenario 3 – In the below example, I’m throwing an exception which is not handled by any catch exception handler specified in the program. Then, parent exception handler will be invoked.

public class MultipleCatchDemo3 {

	public static void main(String[] args) {
		try {
			int a[] = new int[10];
			System.out.println(a[10]);

		} catch (ArithmeticException e) {
			System.out.println("Arithmetic Exception occurs");
		
		} catch (NullPointerException e) {
			System.out.println("NullPointer Exception occurs");
		
		} catch (Exception e) {
			System.out.println("Parent Exception occurs");
		
		}
		System.out.println("Rest of the program");
	}
}

Output
Parent Exception occurs
Rest of the program

Scenario 4 – In the below example, we do not maintain the order of preference in exceptions (child to parent), then compile time error occurs.

public class MultipleCatchDemo4 {

	public static void main(String[] args) {
		try {
			int a[] = new int[10];
			System.out.println(a[10]);

		} catch (Exception e) {
			System.out.println("Parent Exception occurs");

		} catch (ArithmeticException e) {
			System.out.println("Arithmetic Exception occurs");

		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("ArrayIndexOutOfBounds Exception occurs");

		}
		System.out.println("Rest of the program");
	}
}

Output
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
	Unreachable catch block for ArithmeticException. It is already handled by the catch block for Exception
	Unreachable catch block for ArrayIndexOutOfBoundsException. It is already handled by the catch block for Exception

	at JavaDemo.Exception.MultipleCatchDemo4.main(MultipleCatchDemo4.java:13)

Scenario 5 – In the below example, try block has 2 exceptions. It will call the first suitable catch block for the first try staement which has thrown the exception.

public class MultipleCatchDemo5 {

	public static void main(String[] args) {
		try {
			int a[] = new int[10];
			a[15] = 30 / 0;
			System.out.println(a[20]);

		} catch (ArithmeticException e) {
			System.out.println("Arithmetic Exception occurs");

		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("ArrayIndexOutOfBounds Exception occurs");

		} catch (Exception e) {
			System.out.println("Parent Exception occurs");

		}
		System.out.println("Rest of the program");
	}
}

Output
Arithmetic Exception occurs
Rest of the program

Similarly, if I interchanged the try statments as shown below the response will also changes.

public class MultipleCatchDemo6 {

	public static void main(String[] args) {
		try {
			int a[] = new int[10];
			System.out.println(a[20]);
			a[15] = 30 / 0;

		} catch (ArithmeticException e) {
			System.out.println("Arithmetic Exception occurs");

		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("ArrayIndexOutOfBounds Exception occurs");

		} catch (Exception e) {
			System.out.println("Parent Exception occurs");

		}
		System.out.println("Rest of the program");
	}
}

Output
ArrayIndexOutOfBounds Exception occurs
Rest of the program
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s