Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Java Exception Multiple Catch Blocks

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:

Try Block Raising Multiple Exceptions
Try Block Raising Multiple Exceptions

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.

Multiple Catch Blocks
Multiple Catch Blocks

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:

Ordering the Catch Blocks
Ordering the Catch Blocks

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:

We write this code to throw three types of exceptions. They are:

  1. Line number 12 throws StringIndexOutOfBoundsException.
  2. Line number 15 throws IndexOutOfBoundsException.
  3. 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:

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

7. Video Demo


Watch this Article as Video

Categories: Java

Tags: , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.