0
0
Javascriptprogramming~10 mins

Finally block in Javascript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Finally block
Start try block
Execute try code
Error?
YesCatch block runs
Execute catch code
Try block ends
Finally block runs
End
The finally block runs after try and catch blocks, no matter if an error happened or not.
Execution Sample
Javascript
try {
  console.log('Try block');
  throw new Error('Oops');
} catch (e) {
  console.log('Catch block');
} finally {
  console.log('Finally block');
}
This code runs try, then catch because of error, then finally always.
Execution Table
StepActionOutputNext Step
1Enter try blockNo output yetExecute console.log('Try block')
2console.log('Try block')Try blockThrow error new Error('Oops')
3throw new Error('Oops')Error thrownJump to catch block
4Enter catch blockNo output yetExecute console.log('Catch block')
5console.log('Catch block')Catch blockEnter finally block
6Enter finally blockNo output yetExecute console.log('Finally block')
7console.log('Finally block')Finally blockEnd execution
💡 Finally block runs last regardless of error; execution ends after finally.
Variable Tracker
VariableStartAfter Step 3After Step 7
Error thrownNoYes (Error object)Handled (catch)
Key Moments - 3 Insights
Does the finally block run if there is no error in the try block?
Yes, the finally block always runs after try, even if no error occurs, as shown by the flow from try to finally in the execution_table.
If an error is thrown in the try block, does the finally block still run?
Yes, even if an error occurs and catch runs, the finally block runs afterward, as seen in steps 3 to 7 in the execution_table.
Can the finally block change the error handling flow?
The finally block runs last and cannot prevent catch from running, but if it throws an error itself, it can affect the flow (not shown here).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed at step 2?
AFinally block
BCatch block
CTry block
DNo output
💡 Hint
Check the Output column for step 2 in the execution_table.
At which step does the catch block start running?
AStep 4
BStep 2
CStep 3
DStep 6
💡 Hint
Look at the Action column to find when catch block is entered.
If the throw statement in try is removed, which step would no longer happen?
AStep 5 (console.log in catch)
BStep 3 (throw error)
CStep 6 (enter finally)
DStep 2 (console.log in try)
💡 Hint
Refer to the execution_table rows where error is thrown and catch runs.
Concept Snapshot
try {
  // code that may throw
} catch (error) {
  // handle error
} finally {
  // always runs
}

- finally runs no matter what
- runs after try and catch
- useful for cleanup
Full Transcript
The finally block in JavaScript runs after the try and catch blocks no matter what happens. If the try block runs without errors, finally still runs. If an error occurs, catch runs, then finally runs. This ensures code in finally always executes, useful for cleanup tasks. The execution steps show try running, error thrown, catch handling error, then finally running last.