0
0
Pythonprogramming~10 mins

Multiple exception handling in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple exception handling
Start try block
Code runs
Exception?
v1
Match exception type?
v2
Handle
Continue after try-except
The program tries code, checks if an exception happens, then matches it to multiple except blocks to handle it or lets it propagate.
Execution Sample
Python
try:
    x = int('abc')
except ValueError:
    print('ValueError caught')
except TypeError:
    print('TypeError caught')
This code tries to convert a string to int, catches ValueError or TypeError separately.
Execution Table
StepActionEvaluationResult
1Enter try blockNo exception yetProceed to next line
2Execute x = int('abc')Raises ValueErrorException raised
3Check except ValueErrorMatches exception typeHandle ValueError
4Execute print('ValueError caught')Prints messageOutput: ValueError caught
5Exit try-exceptHandled exceptionContinue program
6EndNo more codeProgram ends
💡 Exception ValueError caught by first except block, program continues normally
Variable Tracker
VariableStartAfter Step 2After Step 5
xundefinedException raised, no assignmentundefined
Key Moments - 2 Insights
Why doesn't the second except block for TypeError run?
Because the exception raised is ValueError, which matches the first except block (see step 3 in execution_table), so Python handles it there and skips the rest.
What happens if no except block matches the exception?
The exception propagates and stops the program unless caught elsewhere. This is shown in the flow where unmatched exceptions go to 'Unhandled -> propagate'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 4?
ATypeError caught
BNo output
CValueError caught
DProgram crashes
💡 Hint
Check the 'Result' column at step 4 in the execution_table.
At which step does the program decide which except block to run?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' and 'Evaluation' columns in execution_table step 3.
If the code raised a TypeError instead, which except block would run?
ATypeError except block
BNo except block, program crashes
CValueError except block
DBoth except blocks
💡 Hint
Refer to the except blocks in the code sample and how exceptions match types.
Concept Snapshot
try:
  code that may raise exceptions
except ExceptionType1:
  handle ExceptionType1
except ExceptionType2:
  handle ExceptionType2

Python checks exceptions in order and runs the first matching except block.
Full Transcript
This example shows how Python runs code inside a try block and watches for exceptions. If an exception happens, Python looks at each except block in order to find one that matches the exception type. When it finds a match, it runs that except block's code and then continues the program normally. If no except block matches, the program stops with an error. In the example, converting 'abc' to int raises a ValueError, which is caught by the first except block. The second except block for TypeError is ignored because the exception type does not match. This way, multiple exceptions can be handled separately and clearly.