0
0
Javaprogramming~10 mins

Try–catch block in Java - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Try–catch block
Start
Try block
Exception occurs?
NoTry block ends normally
Yes
Catch block
Continue after catch
End
The program tries code in the try block. If an error happens, it jumps to the catch block to handle it, then continues.
Execution Sample
Java
try {
    int result = 10 / 0;
    System.out.println("Result: " + result);
} catch (ArithmeticException e) {
    System.out.println("Cannot divide by zero!");
}
This code tries to divide 10 by 0, which causes an error, so it prints a message from the catch block.
Execution Table
StepActionEvaluationResult
1Enter try blockint result = 10 / 0Error: ArithmeticException (divide by zero)
2Exception caughtCatch block for ArithmeticExceptionPrint: 'Cannot divide by zero!'
3Continue after catchNo more code in try-catchProgram ends normally
💡 Exception occurs at division, caught by catch block, program continues after catch.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
resultundefinedError (not assigned)undefinedundefined
Key Moments - 2 Insights
Why doesn't the program crash when dividing by zero?
Because the error is caught in the catch block (see execution_table step 2), so the program handles it instead of crashing.
Is the variable 'result' assigned when the exception happens?
No, the assignment fails at step 1 due to the exception, so 'result' remains undefined (see variable_tracker).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 1?
AAn exception occurs during division
BThe division succeeds and result is assigned 0
CThe catch block runs first
DThe program ends
💡 Hint
Check execution_table row 1 where division causes an ArithmeticException
According to variable_tracker, what is the value of 'result' after step 2?
A10
Bundefined
C0
DException object
💡 Hint
See variable_tracker row for 'result' after step 2, it remains undefined because assignment failed
If the division was 10 / 2 instead, what would happen in the execution table?
AException occurs and catch block runs
BProgram crashes immediately
CTry block ends normally, catch block is skipped
DCatch block runs twice
💡 Hint
Without exception, the catch block is skipped (see concept_flow and execution_table logic)
Concept Snapshot
try {
  // code that might cause error
} catch (ExceptionType e) {
  // code to handle error
}

- Code in try runs first.
- If error occurs, catch runs.
- Program continues after catch.
Full Transcript
This example shows how a try-catch block works in Java. The program tries to divide 10 by 0 inside the try block. This causes an ArithmeticException error. Instead of crashing, the program jumps to the catch block that handles this specific error. The catch block prints a message: 'Cannot divide by zero!'. After that, the program continues normally. The variable 'result' is never assigned because the error happens during its assignment. This way, try-catch helps programs handle errors safely and keep running.