# Exception Hierarchy in case of multiple catch blocks.

Java 8Object Oriented ProgrammingProgramming

An exception is an issue (run time error) that occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.

## Multiple exceptions in a code

Whenever we have a code that may generate more than one exception and if you need to handle them specifically you can use multiple catch blocks on a single try.

try{
//code
} catch(Exception1 ex1) {
//
} catch(Exception2 ex2) {
//
}

## Example

Live Demo

import java.util.Arrays;
import java.util.Scanner;
public class MultipleCatchBlocks {
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
int[] arr = {10, 20, 30, 2, 0, 8};
System.out.println("Array: "+Arrays.toString(arr));
System.out.println("Choose numerator and denominator (not 0) from this array (enter positions 0 to 5)");
int a = sc.nextInt();
int b = sc.nextInt();
try {
int result = (arr[a])/(arr[b]);
System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result);
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Warning: You have chosen a position which is not in the array");
}
catch(ArithmeticException e) {
System.out.println("Warning: You cannot divide a number with 0");
}
}
}

## Output1

Enter 3 integer values one by one:
Array: [10, 20, 30, 2, 0, 8]
Choose numerator and denominator(not 0) from this array (enter positions 0 to 5)
2
8
Warning: You have chosen a position which is not in the array

## Output2

Enter 3 integer values one by one:
Array: [10, 20, 30, 2, 0, 8]
Choose numerator and denominator (not 0) from this array (enter positions 0 to 5)
1
4
Warning: You cannot divide a number with 0

## Order of exceptions

If you have multiple catch blocks for a single try and if the exceptions classes of them belong to the same hierarchy, You need to make sure that the catch block that catches the exception class of higher-level is at last at the last in the order of catch blocks.

If not, all the exceptions of the subclasses will be handled in the higher level catch block making the remaining (catch) blocks unreachable leading to a compile-time exception.

#### Example

In the following Java program, a single try block contains 4 catch blocks handling IndexOutOfBoundsException, ArrayIndexOutOfBoundsException, Exception and, ArithmeticException in the same order.

Since IndexOutOfBoundsException is a superclass of ArrayIndexOutOfBoundsException exceptions related to both classes are handled in the first catch block making the later one non-reachable.

Since Exception is the superclass of all the exception classes, if you place the catch block that catches it earlier to the catch blocks catching any other exceptions, all exceptions are handled in the Exception block itself making the remaining blocks unreachable.

Live Demo

import java.util.Arrays;
import java.util.Scanner;
public class ExceptionHierarchy {
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
int[] arr = {10, 20, 30, 2, 0, 8};
System.out.println("Array: "+Arrays.toString(arr));
System.out.println("Choose numerator and denominator (positions 0 to 5)");
int a = sc.nextInt();
int b = sc.nextInt();
try {
int result = (arr[a])/(arr[b]);
System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result);
}
catch(IndexOutOfBoundsException e) {
System.out.println("Warning: position chosen is not in the array");
}
catch(ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
catch(Exception e) {
e.printStackTrace();
}
catch(ArithmeticException e) {
System.out.println("Warning: You cannot divide a number with 0");
}
}
}

#### Output

Compile-time exception

ExceptionHierarchy.java:16: error: exception ArrayIndexOutOfBoundsException has already been caught
}catch(ArrayIndexOutOfBoundsException e) {
^
ExceptionHierarchy.java:20: error: exception ArithmeticException has already been caught
}catch(ArithmeticException e) {
^
2 errors

If you compile the above code in eclipse it generates the following errors −

Message1

Message2

## Solution

To make the above code work,

• The catch block that catches the IndexOutOfBoundsException should be placed after the catch block catching the ArrayIndexOutOfBoundsException.

• The catch block catching the Exception object should be placed at last in the order of the catch blocks.

## Example

Live Demo

import java.util.Arrays;
import java.util.Scanner;
public class ExceptionHierarchy {
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
int[] arr = {10, 20, 30, 2, 0, 8};
System.out.println("Array: "+Arrays.toString(arr));
System.out.println("Choose numerator and denominator (enter positions 0 to 5)");
int a = sc.nextInt();
int b = sc.nextInt();
try {
int result = (arr[a])/(arr[b]);
System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result);
}
catch(ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
catch(IndexOutOfBoundsException e) {
System.out.println("Warning: You have chosen a position which is not in the array");
}
catch(ArithmeticException e) {
System.out.println("Warning: You cannot divide a number with 0");
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Published on 10-Sep-2019 15:58:11