English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List (List)

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input Output (I/O)

Java Reader/Writer

Java Other Topics

Java Catch and Handle Multiple Exceptions

In this tutorial, we will learn how to handle multiple exceptions in Java with examples.

In Java 7Before that, even if there was code redundancy, we had to write multiple exception handling codes for different types of exceptions.

Let's take an example.

Example1: Multiple catch blocks

class Main {
  public static void main(String[] args) {
    try {
      int array[] = new int[10];
      array[10]= 30 / 0;
    { catch (ArithmeticException e) {
      System.out.println(e.getMessage());
    { catch (ArrayIndexOutOfBoundsException e) {
      System.out.println(e.getMessage());
    } 
  }
}

Output Result

/ by zero

}

  • In this example, two exceptions may occur: -  ArithmeticException

  • because we tried to divide a number by 0. -  ArrayIndexOutOfBoundsException9because we have declared a new integer array with boundaries from 0 to10and try to assign a value to the index

We print the exception message in both catch blocks, which is repeated code.

The associativity of the assignment operator (=) is from right to left, so the ArithmeticException is associated with the message / by zero throws.

Handling multiple exceptions in the catch block

In Java SE 7And higher versions, now we can catch multiple types of exceptions in a single catch block.

Each type of exception that can be handled by the catch block is separated by a vertical bar (|).

Its syntax is:

try {
  // code
} catch (ExceptionType1 | Exceptiontype2 ex) { 
  // catch block
}

Example2: Catching multiple exceptions in a single catch block

class Main {
  public static void main(String[] args) {
    try {
      int array[] = new int[10];
      array[10]= 30 / 0;
    { catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
      System.out.println(e.getMessage());
    }
  }
}

Output Result

/ by zero

Catching multiple exceptions in a single catch block can reduce code redundancy and improve efficiency.

The bytecode generated when compiling this program will be smaller than that of a program with multiple catch blocks, as there is no code redundancy.

Note:If a catch block handles multiple exceptions, the catch parameter is implicitly final. This means that we cannot assign any value to catch the parameter.

Catch basic exceptions

When catching multiple exceptions in a single catch block, this rule generalizes to the specialization rule.

This means that if there is an exception hierarchy in the catch block, we can only catch the basic exception and cannot catch multiple specific exceptions.

Let's take an example.

Example3: Only catch the basic exception class

class Main {
  public static void main(String[] args) {
    try {
      int array[] = new int[10];
      array[10]= 30 / 0;
    { catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }
}

Output Result

/ by zero

We know that all exception classes are subclasses of the Exception class. Therefore, we do not need to catch multiple specific exceptions, but only catch the Exception class.

If a basic exception class has been specified in the catch block, do not use subclass exceptions in the same catch block. Otherwise, we will get a compilation error.

Let's take an example.

Example4: Catching Base Class and Subclass Exceptions

class Main {
  public static void main(String[] args) {
    try {
      int array[] = new int[10];
      array[10]= 30 / 0;
    } catch (Exception | ArithmeticException | ArrayIndexOutOfBoundsException e) {
      System.out.println(e.getMessage());
    }
  }
}

Output Result

Main.java:6: error: Alternatives in a multi-catch statement cannot be related by subclassing

In this example, ArithmeticException and ArrayIndexOutOfBoundsException are subclasses of the Exception class. Therefore, we throw a compilation error.