0
0
Javaprogramming~10 mins

Finally block in Java - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Finally block
Start try block
Execute try code
Exception?
YesCatch block executes
Catch code runs
Try code ends
Catch ends
Finally block runs regardless
Program continues
The finally block runs after try and catch blocks, no matter if an exception happened or not.
Execution Sample
Java
try {
    System.out.println("Try block");
    int a = 5 / 0;
} catch (ArithmeticException e) {
    System.out.println("Catch block");
} finally {
    System.out.println("Finally block");
}
This code tries to divide by zero, catches the error, and always runs the finally block.
Execution Table
StepActionEvaluationOutput
1Enter try blockNo exception yetTry block
2Execute division 5 / 0ArithmeticException thrown
3Catch ArithmeticExceptionCatch block runsCatch block
4Execute finally blockRuns regardless of exceptionFinally block
5End of try-catch-finallyProgram continues
💡 Finally block runs always, then program continues after try-catch-finally
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
aundefinedundefinedException thrown, no valueundefinedundefined
Key Moments - 3 Insights
Does the finally block run if an exception is thrown and caught?
Yes, as shown in step 4 of the execution table, the finally block runs no matter what.
What happens if no exception occurs in the try block?
The finally block still runs after the try block finishes, even if catch is skipped (not shown here but same rule).
Can the finally block change the program flow by itself?
Usually no, finally runs after try/catch, but if it contains return or throws, it can affect flow (not shown here).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed at step 3?
AFinally block
BTry block
CCatch block
DNo output
💡 Hint
Check the 'Output' column at step 3 in the execution table.
At which step does the finally block execute?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look for the step where 'Finally block' is printed in the output column.
If the division 5 / 0 did not throw an exception, which step would be skipped?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Catch block runs only if exception occurs, see step 3 in execution table.
Concept Snapshot
try {
  // code that might throw
} catch(Exception e) {
  // handle error
} finally {
  // always runs
}

The finally block runs after try and catch, no matter what happens.
Full Transcript
This example shows how Java's finally block works. The try block runs first and tries to divide by zero, which causes an ArithmeticException. The catch block catches this error and prints 'Catch block'. Then, the finally block runs and prints 'Finally block' regardless of the exception. This means finally always runs after try and catch, whether an error happened or not. Variables like 'a' do not get assigned if an exception occurs during their calculation. This flow helps ensure cleanup or important code runs no matter what.