0
0
Pythonprogramming~10 mins

Try–except execution flow in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Try–except execution flow
Start try block
Execute code in try
Error?
NoContinue after try-except
Yes
Go to except block
Handle error
Continue after try-except
The program tries to run code inside the try block. If an error happens, it jumps to the except block to handle it, then continues.
Execution Sample
Python
try:
    x = 5 / y
except ZeroDivisionError:
    x = 0
print(x)
This code tries to divide 5 by y. If y is zero, it catches the error and sets x to 0, then prints x.
Execution Table
StepActionEvaluationResult
1Enter try blockExecute 'x = 5 / y'Error: NameError (y not defined)
2Error caught?No, NameError not caught by except ZeroDivisionErrorProgram crashes (no except for NameError)
3If y=0 and defined, execute 'x = 5 / 0'ZeroDivisionError raisedJump to except block
4In except ZeroDivisionError blockSet x = 0x is now 0
5After try-exceptPrint xOutput: 0
💡 Execution stops if error is not caught; otherwise continues after except block.
Variable Tracker
VariableStartAfter Step 1After Step 4Final
xundefinederror (not set)00
yundefinedundefinedundefinedundefined or 0 if defined
Key Moments - 3 Insights
Why does the program crash when y is not defined?
Because the except block only catches ZeroDivisionError, but the error is NameError, so it is not caught (see execution_table step 2).
What happens if y is zero?
A ZeroDivisionError occurs, which is caught by the except block, so x is set to 0 and the program continues (see execution_table steps 3 and 4).
Does the except block run if no error happens?
No, the except block only runs if an error occurs in the try block (see execution_table step 1 no error path).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what error occurs at step 1 if y is not defined?
ANameError
BZeroDivisionError
CSyntaxError
DNo error
💡 Hint
Check the 'Evaluation' column in step 1 of the execution_table.
At which step does the except block set x to 0?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Action' and 'Result' columns in step 4 of the execution_table.
If y is 2, what will be printed after the try-except?
A0
B2
C2.5
DAn error occurs
💡 Hint
If no error occurs, the try block runs fully; check what 'x' is assigned in the code sample.
Concept Snapshot
try:
  # code that might cause error
except ErrorType:
  # code to handle error

- Runs try block first
- If error matches except, runs except block
- Otherwise error stops program
- Continues after try-except if handled
Full Transcript
This visual shows how Python runs code inside a try block. If an error happens, it checks if the except block matches the error type. If yes, it runs the except block to handle it and continues. If no, the program stops with an error. For example, dividing by zero raises ZeroDivisionError, which can be caught. But if a variable is not defined, NameError occurs and is not caught by except ZeroDivisionError, so the program crashes. The variable x changes only if the except block runs. This helps keep programs running even when errors happen.