Java Exceptions  «Prev  Next»

Lesson 6Multiple Catch Blocks in Java
ObjectiveHandling more than one type of Exception.

Multiple Catch Clauses in Java

Define the concept of multiple catch blocks in Java to explicitly define a control flow for errors.
In some cases, more than one exception could be raised by a single piece of code. To handle this type of situation, you can specify two or more catch clauses, each catching a different type of exception. When an exception is thrown, each catch statement is inspected in order, and the first one whose type matches that of the exception is executed. After one catch statement executes, the others are bypassed, and execution continues after the try / catch block.
The code below is an example of multiple catch clauses in Java.
Each `catch` block is handling a different type of exception.
public class TestClass {
  public static void main(String args[]) {
      try {
          m1();
      } catch (IndexOutOfBoundsException e) {
          System.out.println("1");
          throw new NullPointerException();
      } catch (NullPointerException e) {
          System.out.println("2");
          return;
      } catch (Exception e) {
          System.out.println("3");
      } finally {
          System.out.println("4");
      }
      System.out.println("END");
  }

  // IndexOutOfBoundsException is a subclass of RuntimeException.
  static void m1() {
      System.out.println("m1 Starts");
      throw new IndexOutOfBoundsException("Big Bang");
  }
}
Program Output:
m1 Starts
Exception in thread "main" 1
4
java.lang.NullPointerException
	at j2eeonline/com.java.exceptions.TestClass.main(TestClass.java:9)
Here's the explanation:
  1. The `try` block calls the `m1()` method, which throws an `IndexOutOfBoundsException`.
  2. The first `catch` block catches `IndexOutOfBoundsException`, prints "1", and then throws a new `NullPointerException`.
  3. The second `catch` block catches the newly thrown `NullPointerException`, prints "2", and then returns, skipping any further execution in the `try-catch` sequence.
  4. The third `catch` block is a generic `Exception` block, which would catch any remaining exceptions not caught by the earlier blocks (but it isn't triggered here).
  5. The `finally` block is always executed regardless of whether an exception is caught or not, so "4" is printed before exiting.

In summary, this code demonstrates multiple catch clauses, where each `catch` block handles a different type of exception.


Handling more than one type of Exception.

If you want to handle more than one type of exception, you can define multiple catch blocks. For example, you could write:
(In the code below, NumberFormatException is a RuntimeException.)
try { 
// try something here 
} catch (NumberFormatException e) { 
// handle number format exceptions here 
} catch (IOException e) { 
// handle IOexceptions here
} 

We said that one way to handle an exception was to rethrow it. Here's an example. Let us say your method converts a string into a number.
Here's how the constructor for Integer that takes a string is declared:
public Integer(String s) throws NumberFormatException 

The documentation notes that this constructor throws the indicated exception if s does not contain a valid number.
Therefore, one possible way to proceed is to place the call to the constructor inside a try block and handle the exception. Yet another way to proceed is to indicate that your own code might generate an exception:
Integer myIntegerCreator(String s) throws NumberFormatException { 
  return new Integer(s); 
}


Catching Exceptions - Exercise

This exercise asks you to modify code to handle a couple of exceptions that could be thrown when using this program.
Catching Exceptions - Exercise

Catching Java Exceptions Quiz

When you finish the exercise, return here to take a quiz on exceptions.
Catching Java Exceptions Quiz

SEMrush Software