0
0
Pythonprogramming~10 mins

Exception hierarchy in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Exception hierarchy
BaseException
Exception
ValueError
ZeroDivisionError
Exceptions in Python form a tree starting from BaseException. Most errors inherit from Exception, which branches into specific error types.
Execution Sample
Python
try:
    x = 1 / 0
except ZeroDivisionError:
    print("Cannot divide by zero")
This code tries to divide by zero and catches the ZeroDivisionError to print a message.
Execution Table
StepActionEvaluationResult
1Execute 'x = 1 / 0'1 / 0Raises ZeroDivisionError
2Catch exceptionIs exception ZeroDivisionError?Yes
3Run except blockprint messageOutputs: Cannot divide by zero
4End try-exceptNo more codeProgram continues normally
💡 Exception caught by ZeroDivisionError handler, program does not crash.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
xundefinedexception raised, no assignmentundefinedundefined
Key Moments - 3 Insights
Why is x not assigned after the division by zero?
Because the exception occurs during the calculation, the assignment never happens (see execution_table step 1).
Why does the program not crash after the error?
The ZeroDivisionError is caught by the except block (execution_table step 2), so the program handles it gracefully.
What if we catch Exception instead of ZeroDivisionError?
Catching Exception would also catch ZeroDivisionError because it is a subclass (see concept_flow showing hierarchy).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 1?
Ax is assigned the value 0
BA ZeroDivisionError is raised
CThe except block runs
DThe program ends
💡 Hint
Check the 'Evaluation' and 'Result' columns in execution_table row 1.
According to variable_tracker, what is the value of x after step 2?
A0
B1
Cundefined
DNone
💡 Hint
See variable_tracker row for x after step 2.
If we replaced 'except ZeroDivisionError' with 'except Exception', what would happen?
AThe exception would be caught and handled
BThe program would crash
CThe exception would not be caught
DA different exception would be raised
💡 Hint
Refer to concept_flow showing Exception is a parent of ZeroDivisionError.
Concept Snapshot
Python exceptions form a hierarchy starting at BaseException.
Most user errors inherit from Exception.
You catch exceptions using try-except blocks.
Catching a parent exception catches all its children.
Example: catching Exception catches ZeroDivisionError.
Use specific exceptions to handle errors precisely.
Full Transcript
In Python, errors are organized in a hierarchy starting from BaseException. Most common errors inherit from Exception. For example, ZeroDivisionError is a child of Exception. When code runs, if an error happens, Python looks for a matching except block to handle it. If you try to divide by zero, Python raises ZeroDivisionError. If you have a try-except block catching ZeroDivisionError, it will run the except code and prevent the program from crashing. Variables are not assigned if an exception happens during their assignment. Catching a parent exception like Exception will also catch its child exceptions like ZeroDivisionError. This helps you handle errors broadly or specifically.