in , ,

Java catch Multiple Exceptions

Java catch Multiple Exceptions
Java catch Multiple Exceptions

Java catch Multiple Exceptions

Java catch Multiple Exceptions: In this tutorial, we will learn to handle numerous exceptions in Java with the help of examples.

Prior to Java 7, we needed to write various exceptions handling codes for various types of exceptions Even of whether there was code repetition.

Let’s take an example.

Example 1: 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

/ by zero

In this example, two exceptions may occur:

  • ArithmeticException because we are trying to divide a number by 0.
  • ArrayIndexOutOfBoundsException because we have declared a new integer array with array bounds 0 to 9 and we are trying to assign a value to index 10.

We are printing out the exception message in both the catch blocks for example copy code.

The associativity of the task operator = is right to left, so an ArithmeticException is thrown first with the message/by zero.


Handle Multiple Exceptions in a catch Block

In Java SE 7 and later, we can now catch more than one type of exception in a single catch block.

Every exception type that can be taken care of by the catch block is isolated using a vertical bar or line |.

Its syntax is:

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

Example 2: Multi-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

/ by zero

Getting numerous exceptions in a single catch block decreases code duplication and builds effectiveness.

The bytecode created while compiling this program will be smaller than the program having numerous catch blocks as there is no code repetition.

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


Catching base Exception

When getting various exemptions in a single catch block, the standard is summed up to specific.

This implies that if there is an order of exemptions in the catch block, we can get the base exception just as opposed to getting different particular exceptions.

Let’s take an example.

Example 3: Catching base exception class only

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

/ by zero

We realize that all the exception classes are subclasses of the Exception class. In this way, rather than getting various particular exemptions, we can basically get the Exception class.


If the base exception class has already been specified in the catch block, do not use child exception classes in the same catch block. Otherwise, we will get a compilation error.

Let’s take an example.

Example 4: Catching base and child exception classes

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

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

In this example, ArithmeticException and ArrayIndexOutOfBoundsException are both subclasses of the Exception class. So, we get a compilation error.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Java throw and throws

Java throw and throws

Java try-with-resources

Java try-with-resources