Java Multiple Catch Blocks: Efficiently Handle Multiple Exceptions
Learn how to use multiple catch blocks in Java to handle different exceptions within a single try block. Discover how this structure allows for flexible error handling, enabling developers to manage various exception types in the same code section efficiently.
Java Multiple Catch Blocks
In Java, multiple catch blocks are used to handle various exceptions that may occur in a particular section of code. A try block can have multiple catch blocks to manage different exceptions.
Syntax: Multiple Catch Blocks
Syntax
try {
// Protected code
} catch (ExceptionType1 e1) {
// Handle ExceptionType1
} catch (ExceptionType2 e2) {
// Handle ExceptionType2
} catch (ExceptionType3 e3) {
// Handle ExceptionType3
}
In this example, the try block is followed by three catch blocks, but there can be any number of catch blocks. The exceptions are handled by the first catch block whose exception type matches the thrown exception. If none match, the exception propagates down the list.
Points to Remember
- Only one exception can be handled at a time. Each
catchblock deals with one specific type of exception.- The order of the
catchblocks is crucial. Specific exceptions should appear before generic ones, or the compiler will generate an error.
Example: Handling Multiple Exceptions
The following code demonstrates the use of multiple catch blocks. In this example, a division by zero causes an exception, and it is caught and handled by the second catch block.
Syntax
package com.example;
public class ExcepTest {
public static void main(String args[]) {
try {
int[] a = new int[2];
int b = 0;
int c = 1 / b; // This will cause ArithmeticException
System.out.println("Access element: " + a[3]); // This will cause ArrayIndexOutOfBoundsException
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException: " + e);
}
catch (ArithmeticException e) {
System.out.println("ArithmeticException: " + e);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
System.out.println("Out of the block");
}
}
Output
ArithmeticException: / by zero
Out of the block
Handling Multiple Exceptions in a Single Catch Block
Since Java 7, it is possible to handle more than one exception in a single catch block. This simplifies the code significantly. Here's an example:
Syntax
catch (IOException | FileNotFoundException ex) {
ex.printStackTrace();
}
Example: Single Catch Block for Multiple Exceptions
The following code shows how you can handle multiple exceptions using a single catch block. In this case, both ArrayIndexOutOfBoundsException and ArithmeticException are caught together.
Syntax
package com.example;
public class ExcepTest {
public static void main(String args[]) {
try {
int[] a = new int[2];
int b = 0;
int c = 1 / b; // This will cause ArithmeticException
System.out.println("Access element: " + a[3]); // This will cause ArrayIndexOutOfBoundsException
}
catch (ArrayIndexOutOfBoundsException | ArithmeticException e) {
System.out.println("Exception: " + e);
}
System.out.println("Out of the block");
}
}
Output
Exception: java.lang.ArithmeticException: / by zero
Out of the block