Syntax of Multiple Catch Blocks
The syntax for using multiple catch blocks in Java is straightforward. Here's how it looks
try {
// Code that may throw exceptions
} catch (ExceptionType1 e1) {
// Handle exception of type ExceptionType1
} catch (ExceptionType2 e2) {
// Handle exception of type ExceptionType2
} catch (ExceptionType3 e3) {
// Handle exception of type ExceptionType3
}
Each catch block is checked in the order they appear, and the first matching catch block is executed.
How Multiple Catch Blocks Work
When an exception occurs in the try block, the JVM checks each catch block from top to bottom to find a matching exception type. Once a matching catch block is found, it executes the code inside that catch block. If no matching catch block is found, the program terminates.
Example 1: Using Multiple Catch Blocks with Different Exceptions
Here is an example of using multiple catch blocks to handle different exceptions:
Java
public class MultipleCatchExample {
public static void main(String[] args) {
try {
int[] numbers = {1, 2, 3};
System.out.println(numbers[3]); // This will throw ArrayIndexOutOfBoundsException
int result = 10 / 0; // This will throw ArithmeticException
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index is out of bounds!");
} catch (ArithmeticException e) {
System.out.println("Arithmetic error: Division by zero!");
} catch (Exception e) {
System.out.println("An unexpected error occurred.");
}
}
}
You can also try this code with Online Java Compiler
Run Code
Explanation:
- The try block contains code that may throw exceptions.
- The first catch block handles ArrayIndexOutOfBoundsException.
- The second catch block handles ArithmeticException.
- The third catch block handles any other exceptions.
Output:
Array index is out of bounds!
Example 2: Catching Subclasses of Exception
Here's an example showing how to handle exceptions that are subclasses of a common exception type:
Java
public class SubclassCatchExample {
public static void main(String[] args) {
try {
String str = null;
System.out.println(str.length()); // This will throw NullPointerException
} catch (NullPointerException e) {
System.out.println("Null pointer exception occurred!");
} catch (Exception e) {
System.out.println("An unexpected error occurred.");
}
}
}
You can also try this code with Online Java Compiler
Run Code
Explanation:
- The try block contains code that may throw a NullPointerException.
- The first catch block handles NullPointerException.
- The second catch block is a generic catch-all for any other exceptions.
Output:
Null pointer exception occurred!
Frequently Asked Questions
What is the difference between multiple catch blocks and a multi-catch block?
Multiple catch blocks handle different exceptions in separate catch statements. A multi-catch block handles multiple exceptions in a single catch statement using the | operator.
Can we have nested try-catch blocks?
Yes, try-catch blocks can be nested to handle exceptions at different levels of code execution.
How to handle multiple exceptions in Java 6 and earlier?
In Java 6 and earlier, you must use multiple catch blocks, as the multi-catch block was introduced in Java 7.
Conclusion
Using multiple catch blocks in Java allows for handling different types of exceptions separately. This makes the code more readable and maintainable. By following best practices and understanding the flow of control, you can effectively manage exceptions in your Java programs.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.