0
0
PHPprogramming~10 mins

Finally block behavior in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Finally block behavior
Try block starts
Code runs in try
Exception?
YesCatch block runs
Catch code runs
Try block ends
Catch ends
Finally block runs ALWAYS
Code after try-catch-finally
The finally block runs no matter what happens in try or catch blocks, even if an exception occurs or not.
Execution Sample
PHP
<?php
try {
  echo "Try\n";
} catch (Exception $e) {
  echo "Catch\n";
} finally {
  echo "Finally\n";
}
This code prints messages from try, catch (if exception), and finally blocks.
Execution Table
StepBlockActionOutputNotes
1TryExecute try block codeTryNo exception thrown
2CatchSkippedNo exception to catch
3FinallyExecute finally blockFinallyRuns always
4AfterContinue after try-catch-finallyProgram continues normally
💡 Execution continues after finally block with rest of program
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Output"""Try\n""Try\n""Try\nFinally\n""Try\nFinally\n"
Key Moments - 3 Insights
Does the finally block run if an exception is thrown?
Yes, the finally block always runs whether or not an exception occurs, as shown in step 3 of the execution_table.
What happens if there is no catch block but a finally block exists?
The finally block still runs after the try block finishes, even if no catch block is present.
Can the finally block change the output even if an exception is thrown?
Yes, the finally block runs after try or catch and can add to output or perform cleanup.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed after the try block runs?
ACatch
BTry
CFinally
DNothing
💡 Hint
Check Step 1 in execution_table under Output column
At which step does the finally block run according to the execution_table?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for 'Finally' block execution in the Step column
If an exception was thrown in the try block, which block would run next before finally?
ACatch block
BFinally block immediately
CTry block again
DProgram ends
💡 Hint
Refer to concept_flow showing exception path to catch block
Concept Snapshot
try { ... } catch (Exception $e) { ... } finally { ... }
- finally block runs always, after try and catch
- runs even if exception occurs or not
- useful for cleanup code
- program continues after finally block
Full Transcript
In PHP, the finally block runs after the try and catch blocks no matter what. If the try block runs without error, the catch block is skipped, but finally still runs. If an exception occurs, the catch block runs, then finally runs. This ensures code in finally always executes, useful for cleanup. The execution table shows try prints 'Try', catch is skipped, then finally prints 'Finally'. The variable tracker shows output grows after try and finally. Key moments clarify that finally runs always, even without catch, and can affect output. The quiz checks understanding of when finally runs and what prints.