Java multiple catch blocks
It is possible to have more than one catch statement in the catch block as illustrated below in the following code segment:
try {
// block of code that generates an exception
}
catch (Exception-Type1 e1) {
// block of code that handles the exception for Exception-Type1
}
catch (Exception-Type2 e2) {
// block of code that handles the exception for Exception-Type2
}
. . .
. . .
catch (Exception-TypeN eN) {
// block of code that handles the exception for Exception-TypeN
}
//Exception-TypeN is the type of exception that has occurred
When an exception in a try block is generated, the Java treats the multiple catch statements like cases in a switch statement. The first catch statement whose parameter matches with the exception object will be executed, and the remaining statements will be skipped.
On exit from a catch block, normal execution continues unless there is any pending exception that has been thrown and not handled. If this is the case, the method is aborted and the exception is propagated up the runtime stack.
Let’s see a coding example (Except04a.java):
public class Except04a {
public static void main(String[] args) {
int a = 10;
int b = 0;
int arr[] = new int[10];
String str = null;
double n = Math.random();
try {
if (n < 0.3) {
int c = a / b;
}
else if (n < 0.6) {
arr[20] = 100;
}
else {
char ch = str.charAt(0);
}
}
catch (ArithmeticException e1) {
System.out.println("Exception : " + e1);
}
catch (ArrayIndexOutOfBoundsException e2) {
System.out.println("Exception : " + e2);
}
catch (NullPointerException e3) {
System.out.println("Exception : " + e3);
}
} //end of main()
} //end of class
The output will vary depending on the value of ‘n’ which is generated randomly by using the random() method of java.lang.Math class.
Multi-catch block
It has been included since Java 7. Here we can modify the code named Except04a.java to include multi-catch block. The modified code is named Except04b.java.
public class Except04b {
public static void main(String[] args) {
int a = 10;
int b = 0;
int arr[] = new int[10];
String str = null;
double n = Math.random();
try {
if (n < 0.3) {
int c = a / b;
}
else if (n < 0.6) {
arr[20] = 100;
}
else {
char ch = str.charAt(0);
}
}
catch (ArithmeticException | ArrayIndexOutOfBoundsException | NullPointerException e) {
System.out.println("Exception : " + e);
}
} //end of main()
} //end of class
Special Case
When we use multiple catch statements, it is important to remember that exception subclass must come before any of its superclass. This is because a catch statement that uses a superclass will catch exceptions of that type plus any of its subclasses. Thus, a subclass would never be reached if it came after its superclass. Further, in Java, unreachable code is an error. For example, consider the following program (Except04c.java):
public class Except04c {
public static void main (String args[]) {
try {
int array[] = new int[100];
array[100] = 100; }
catch (Exception e) {
System.out.println("Exception the Superclass");
} catch (ArrayIndexOutOfBoundsException e) { System.out.println("Subclass of Exception");
} }
}
Output:
Compile time error
If we try to compile this program, we will receive an error message stating that the second catch statement is unreachable because the exception has already been caught. Since ArrayIndexOutOfBoundsException is a subclass of Exception, the first catch statement will handle all Exception-based errors, including ArrayIndexOutOfBoundsException. This means that the second catch statement will never execute. To fix the problem, we have to reverse the order of the catch statements, which is done in the modified program named Except04d.java.
public class Except04d {
public static void main (String args[]) {
try {
int array[] = new int[100];
array[100] = 100;
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Subclass of Exception");
}
catch (Exception e) {
System.out.println("Exception the Superclass");
}
}
}
The output of above program is as follows:
Output:
Subclass of Exception