0
0
Javaprogramming~10 mins

Exception propagation in Java - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Exception propagation
Exception occurs in method
Is exception caught here?
NoPass exception to caller
|Yes
Handle exception
Continue normal flow
When an exception happens, Java checks if the current method handles it. If not, it passes the exception up to the caller until caught or program ends.
Execution Sample
Java
public class Test {
  static void methodA() {
    methodB();
  }
  static void methodB() {
    throw new RuntimeException("Error");
  }
  public static void main(String[] args) {
    methodA();
  }
}
This code throws an exception in methodB, which is not caught there, so it propagates up to main and stops the program.
Execution Table
StepMethodActionException StatePropagation
1mainCalls methodA()No exceptionNo
2methodACalls methodB()No exceptionNo
3methodBThrows RuntimeExceptionException thrownNo catch here, propagate up
4methodANo catch blockException propagatesPass to caller (main)
5mainNo catch blockException propagatesProgram terminates with error
💡 Exception not caught in any method, program stops with RuntimeException
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
exceptionnullnullnullRuntimeException thrownUncaught exception
Key Moments - 2 Insights
Why does the exception move from methodB to methodA?
Because methodB does not have a catch block, the exception automatically moves up to the caller methodA as shown in step 3 and 4 of the execution_table.
What happens if main had a try-catch block?
If main caught the exception, the propagation would stop there and the program could handle the error gracefully, preventing termination as shown in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the exception first thrown?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Check the 'Exception State' column to see when the exception appears.
At which step does the exception propagate from methodA to main?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look for 'Pass to caller (main)' in the 'Propagation' column.
If methodB had a try-catch block, how would the 'exception' variable change in variable_tracker?
AIt would be null after step 5
BIt would be RuntimeException thrown after step 3
CIt would remain null after step 3
DIt would be RuntimeException thrown after step 5
💡 Hint
If caught in methodB, exception does not propagate up, so variable_tracker shows no uncaught exception.
Concept Snapshot
Exception propagation in Java:
- Exception thrown in a method
- If not caught, passes to caller method
- Continues up call stack until caught or program ends
- Uncaught exceptions terminate the program
- Use try-catch to handle exceptions and stop propagation
Full Transcript
This example shows how an exception thrown in methodB moves up to methodA and then main because none catch it. The program stops with an error. Exception propagation means passing the error up the call chain until handled or program ends.