Multi-catch Block in Java


Use multi-catch blocks to catch multiple types of exceptions in a single catch block, reducing code duplication.

Source Code

try {
    int[] myNumbers = {1, 2, 3};
    System.out.println(myNumbers[10]);
} catch (IndexOutOfBoundsException | NullPointerException e) {
    System.out.println("Exception caught: " + e);
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments