1. Introduction to Multiple Catch Block
In the last article, we saw how to handle the exception using
try & catch blocks. The exception raised in the
try block will search for a suitable
catch block by matching it with the catch block’s exception type. More specific
catch block can have more precise knowledge about the exception. For Example, Exception class is more general and ArithmeticException
class is more specific. This means, the
catch block with more generic Exception can denote any cause like File Not Found, Stack Overflow, Arithmetic Operation exception. But the
catch block for
ArithmeticException will pinpoint the exact cause of failure. Now, these handling blocks for specific exception leads to Multiple Catch Blocks for a single try block. In this example, we will study about Multiple Catch Blocks.
2. Try Block Raising Multiple Exceptions
We know that a
try block in Java will have a matching catch
block. Now, we see how to use multiple catch
blocks. Have a look at the below picture:

The picture shows there are three code snippets within the try
block. Let us assume code snippet 2 depends on code snippet 1 and code snippet 3 depends on code snippet 2. We will also assume each code snippet has the possibility of one exception type. From the picture, we can assume the exception types are A, B and C.
3. Need for Multiple Catch Blocks
In Java, Exception is the base class for all the other Exceptions. So, we can have a single catch
block with Exception type to handle all those exceptions. But the flip side is, we cannot say which exception type we are handling in the catch
block. This will make us to provide separate catch block handler for each exception types say A, B & C. The below picture shows three catch blocks for handling the exception types.

4. Ordering the Catch Blocks
In the past section, we saw three catch
blocks. In Java and most oops supported languages, ordering of the catch block is crucial. Above picture shows we ordered the handlers as A, B, and C. When we perform the ordering, most specific handlers should stay on top. Also, the most generalized exception blocks should stay in the bottom. To perform this ordering, we must know the inheritance relation between the exception types. Now look at the below picture:

The picture states the Exceptions M and N are two types of exception and they are not related to each other. Moreover, they are not related to the Exceptions A, B and C as well. But the Exceptions A, B and C are related through inheritance. Exception A is the topmost class and C is most derived class. Let us say the try block will have the possibility of throwing all these 5 Exceptions.
Now, when we form the multiple catch
blocks, we can place the Exception M & N anywhere in the ordering. Because of inheritance, we must place Exception C in the top before A and B. The Exception A should stay in the bottom as it is the base class of B & C. The picture shows how we stack these five exceptions when we are ordering it in the catch blocks. Note, we can place Exceptions M and N anywhere. But the order of C, B, A should not be changed.
5. Code Example Multiple Catch Blocks
Below is the code which is using the try block:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//Sample 01: Declarations Method Level String str1 = "Sample String"; int[] x = {1,2,3,4,5,6,7,8,9,10}; int a = 10; int b = 0; try { //Sample 01: String Throws Index based Exception char c = str1.charAt(100); //Sample 02: Array throwing an Index Related Exception int y = x[15]; //Sample 03: Arithmetic Exception int z = a / b; } |
We write this code to throw three types of exceptions. They are:
- Line number 12 throws StringIndexOutOfBoundsException.
- Line number 15 throws IndexOutOfBoundsException.
- The expression at line number 18 throws ArithmeticException.
We can handle all these Exceptions with one catch block by using the generalized Exception class. But, to have a specific handling code, we can provide catch block for each specific Exception Type. The catch block look as in the below code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
//Sample 04: Ordering the catch block Handler catch (StringIndexOutOfBoundsException ex) { System.out.println("The String Accessed is => " + str1); System.out.println(ex.getMessage()); } catch (IndexOutOfBoundsException ae) { System.out.println("The array length is => " + x.length); System.out.println("Index Referred is " + ae.getMessage()); } catch (ArithmeticException ae) { System.out.println(ae.getMessage()); System.out.println("Division attempted is => " + a + "/" + b); } catch (Exception e) { System.out.println(e.getMessage()); } |
Here, we placed the more specific catch
block
StringIndexOutOfBoundsException in the top.
StringIndexOutOfBoundsException and
IndexOutOfBoundsException falls under the inheritance relation. So the handler for string index exception precedes the more generalized index out-of-bound exception. The
ArithmeticException is not related to out of bound Exceptions. So we can keep it anywhere in the ordering but above the handler for Exception. Since
Exception is the base class for all exception types, it is good practice to place that last in the multiple catch
block ordering.
6. Complete Code Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
public class MainEntry { public static void main(String args[]) { //Sample 01: Declarations Method Level String str1 = "Sample String"; int[] x = {1,2,3,4,5,6,7,8,9,10}; int a = 10; int b = 0; try { //Sample 01: String Throws Index based Exception char c = str1.charAt(100); //Sample 02: Array throwing an Index Related Exception int y = x[15]; //Sample 03: Arithmetic Exception int z = a / b; } //Sample 04: Ordering the catch block Handler catch (StringIndexOutOfBoundsException ex) { System.out.println("The String Accessed is => " + str1); System.out.println(ex.getMessage()); } catch (IndexOutOfBoundsException ae) { System.out.println("The array length is => " + x.length); System.out.println("Index Referred is " + ae.getMessage()); } catch (ArithmeticException ae) { System.out.println(ae.getMessage()); System.out.println("Division attempted is => " + a + "/" + b); } catch (Exception e) { System.out.println(e.getMessage()); } } } |
7. Video Demo
Categories: Java
Tags: Catch Block, catch block order, multiple catch block, Try Block