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.
try: x = 1 / 0 except ZeroDivisionError: print("Cannot divide by zero")
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Execute 'x = 1 / 0' | 1 / 0 | Raises ZeroDivisionError |
| 2 | Catch exception | Is exception ZeroDivisionError? | Yes |
| 3 | Run except block | print message | Outputs: Cannot divide by zero |
| 4 | End try-except | No more code | Program continues normally |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| x | undefined | exception raised, no assignment | undefined | undefined |
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.