0
0
Javaprogramming~10 mins

Multiple catch blocks in Java - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple catch blocks
Try block starts
Exception occurs?
NoTry block ends normally
Yes
Check first catch block
Matches exception?
NoCheck next catch block
Yes
Execute catch block
Continue after try-catch
The program tries code in the try block. If an exception happens, it checks each catch block in order to find a match and runs the matching one.
Execution Sample
Java
try {
    int[] arr = new int[2];
    System.out.println(arr[5]);
} catch (ArrayIndexOutOfBoundsException e) {
    System.out.println("Index error");
} catch (Exception e) {
    System.out.println("General error");
}
This code tries to access an invalid array index, catches the specific index error first, and prints a message.
Execution Table
StepActionException ThrownCatch Block CheckedCatch Block Matches?Output
1Enter try blockNoneNoneN/A
2Access arr[5]ArrayIndexOutOfBoundsExceptionFirst catch (ArrayIndexOutOfBoundsException)Yes
3Execute first catch blockHandledFirst catchYesIndex error
4Exit try-catchHandledNoneN/A
💡 Exception handled by first catch block, program continues after try-catch.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
arrnullnew int[2]new int[2]new int[2]new int[2]
ExceptionnonenoneArrayIndexOutOfBoundsExceptionhandlednone
Key Moments - 2 Insights
Why does the first catch block run instead of the second?
Because the exception thrown is ArrayIndexOutOfBoundsException, which matches the first catch block exactly, so Java runs it and skips the others (see execution_table step 2 and 3).
What happens if no catch block matches the exception?
The exception would propagate up and could crash the program if not handled elsewhere. In this example, all exceptions are caught by the second catch block if the first doesn't match.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which catch block handles the exception at step 3?
AThe first catch block (ArrayIndexOutOfBoundsException)
BThe second catch block (Exception)
CNo catch block handles it
DBoth catch blocks handle it
💡 Hint
Check the 'Catch Block Matches?' and 'Output' columns at step 3 in the execution_table.
At which step is the exception first detected?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Exception Thrown' column in the execution_table.
If the first catch block was removed, which catch block would handle the exception?
ANo catch block would handle it
BA new catch block would be needed
CThe second catch block (Exception)
DThe program would crash immediately
💡 Hint
See that the second catch block catches Exception, which is a parent of ArrayIndexOutOfBoundsException.
Concept Snapshot
try {
  // code that might throw exceptions
} catch (SpecificException e) {
  // handle specific exception
} catch (GeneralException e) {
  // handle other exceptions
}
Multiple catch blocks let you handle different errors separately, checked in order.
Full Transcript
This example shows how Java tries code inside a try block. If an error happens, it looks at each catch block in order to find one that matches the error type. The first matching catch block runs, handling the error. Here, accessing an invalid array index throws ArrayIndexOutOfBoundsException, which matches the first catch block, so it prints 'Index error'. The program then continues normally after the try-catch. If no catch block matches, the error would go up and might crash the program. Multiple catch blocks let you handle different errors in different ways.