Can a method have multiple catch blocks?
January 16, 2025
Yes, a method can have multiple catch blocks to handle different exception types.
try {
int[] arr = new int[2];
arr[5] = 10;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(“Index out of bounds!”);
} catch (Exception e) {
System.out.println(“Generic exception caught.”);
}