0
0
Pythonprogramming~10 mins

Try–except–finally behavior in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Try–except–finally behavior
Start try block
Execute try code
Exception?
NoExecute finally block
Go to except block
Execute except block
Execute finally block
End
The program tries the code in try. If no error, it runs finally then ends. If error, it runs except, then finally, then ends.
Execution Sample
Python
try:
    x = 1 / y
except ZeroDivisionError:
    x = 0
finally:
    print(x)
This code tries to divide 1 by y, handles division errors by setting x to 0, and always prints x at the end.
Execution Table
StepActionEvaluationResult
1Enter try blockExecute 'x = 1 / y'Error: NameError (y not defined)
2Exception caught?No ZeroDivisionError, but NameError raisedNo except block matches, skip except
3Execute finally blockPrint xError: x not assigned, NameError raised again
4Program endsUnhandled NameErrorProgram crashes with NameError
💡 No matching except block for NameError, finally runs, then program crashes
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
xundefinedundefined (error on assignment)undefinedundefinedundefined (program crashes)
yundefinedundefinedundefinedundefinedundefined
Key Moments - 3 Insights
Why does the program crash even though there is an except block?
The except block only catches ZeroDivisionError, but the error is NameError because y is not defined. See execution_table step 2.
Does the finally block run if an exception is not caught?
Yes, finally always runs no matter what. See execution_table step 3 where finally runs even though exception is unhandled.
What happens if the except block matches the error?
If except matches, it runs the except code, then finally runs. The program does not crash. This is not shown here but is the normal flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what error occurs at step 1?
ANo error occurs
BZeroDivisionError because of division by zero
CNameError because y is not defined
DSyntaxError due to wrong code
💡 Hint
Check the 'Evaluation' column in step 1 of the execution_table
At which step does the finally block execute?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for 'Execute finally block' in the Action column of execution_table
If the except block caught NameError instead, what would change in the execution?
AThe except block would run, then finally, and program ends normally
BThe program would crash after finally
CThe finally block would not run
DThe try block would run again
💡 Hint
Refer to key_moments explanation about except block matching the error
Concept Snapshot
try:
  code that might fail
except ErrorType:
  handle error
finally:
  code that always runs

- try runs first
- except runs if matching error
- finally runs always
- program ends after finally
Full Transcript
This example shows how Python runs try-except-finally. The try block runs code that may cause an error. If an error happens and matches the except type, except runs. Finally always runs no matter what. Here, a NameError happens because y is not defined. The except block only catches ZeroDivisionError, so it does not run. Finally runs and tries to print x, but x was never set, so the program crashes with NameError. This shows that finally runs even if except does not catch the error. If except matched the error, it would run, then finally, and the program would end normally.