Multiple Catch Blocks
When working with exceptions in Java, you often need to handle different types of errors that may occur in your code. Multiple catch blocks allow you to respond to various exception types separately within a single try-catch structure. By placing several catch blocks after a try block, you can specify unique handling logic for each exception class. This approach makes your code more robust, as you can address specific problems such as input errors, arithmetic mistakes, or null references differently, depending on the situation.
try {
// code
} catch (Exception1) {
// exception handling 1
} catch (Exception2) {
// exception handling 2
}
The order of catch blocks is crucial because Java checks each catch block in sequence, from top to bottom, to find a matching exception type.
Since exceptions are organized in a hierarchy, a catch block for a superclass exception (like Exception) must always come after catch blocks for its subclasses (such as NullPointerException or ArithmeticException).
If a superclass catch block appears before its subclasses, the compiler will generate an error, because the subclass catch blocks would become unreachable. Understanding this hierarchy ensures that your program handles exceptions in the most precise way possible.
Main.java
12345678910111213141516package com.example; public class Main { public static void main(String[] args) { try { int[] numbers = {1, 2, 3}; System.out.println("Result: " + (10 / numbers[3])); } catch (ArithmeticException e) { System.out.println("ArithmeticException caught: " + e.getMessage()); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBoundsException caught: " + e.getMessage()); } catch (Exception e) { System.out.println("General Exception caught: " + e.getMessage()); } } }
In the program above, the try block attempts to divide 10 by the value at index 3 of the numbers array. However, since the array only has three elements (indices 0, 1, and 2), accessing numbers[3] will throw an ArrayIndexOutOfBoundsException.
When an exception occurs, Java checks each catch block in order. It first looks for an ArithmeticException, then an ArrayIndexOutOfBoundsException, and finally a general Exception. Because the thrown exception matches ArrayIndexOutOfBoundsException, only that catch block executes, and the others are skipped. This demonstrates how Java chooses the most specific matching catch block, ensuring that the exception is handled appropriately.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 10
Multiple Catch Blocks
Swipe to show menu
When working with exceptions in Java, you often need to handle different types of errors that may occur in your code. Multiple catch blocks allow you to respond to various exception types separately within a single try-catch structure. By placing several catch blocks after a try block, you can specify unique handling logic for each exception class. This approach makes your code more robust, as you can address specific problems such as input errors, arithmetic mistakes, or null references differently, depending on the situation.
try {
// code
} catch (Exception1) {
// exception handling 1
} catch (Exception2) {
// exception handling 2
}
The order of catch blocks is crucial because Java checks each catch block in sequence, from top to bottom, to find a matching exception type.
Since exceptions are organized in a hierarchy, a catch block for a superclass exception (like Exception) must always come after catch blocks for its subclasses (such as NullPointerException or ArithmeticException).
If a superclass catch block appears before its subclasses, the compiler will generate an error, because the subclass catch blocks would become unreachable. Understanding this hierarchy ensures that your program handles exceptions in the most precise way possible.
Main.java
12345678910111213141516package com.example; public class Main { public static void main(String[] args) { try { int[] numbers = {1, 2, 3}; System.out.println("Result: " + (10 / numbers[3])); } catch (ArithmeticException e) { System.out.println("ArithmeticException caught: " + e.getMessage()); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBoundsException caught: " + e.getMessage()); } catch (Exception e) { System.out.println("General Exception caught: " + e.getMessage()); } } }
In the program above, the try block attempts to divide 10 by the value at index 3 of the numbers array. However, since the array only has three elements (indices 0, 1, and 2), accessing numbers[3] will throw an ArrayIndexOutOfBoundsException.
When an exception occurs, Java checks each catch block in order. It first looks for an ArithmeticException, then an ArrayIndexOutOfBoundsException, and finally a general Exception. Because the thrown exception matches ArrayIndexOutOfBoundsException, only that catch block executes, and the others are skipped. This demonstrates how Java chooses the most specific matching catch block, ensuring that the exception is handled appropriately.
Thanks for your feedback!