What if you could catch many errors with just one simple rule instead of dozens of checks?
Why Exception hierarchy in Python? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you are writing a program that reads files, connects to the internet, and processes data. You try to catch errors one by one, writing separate code for each possible problem like file not found, connection lost, or wrong data format.
This manual way is slow and confusing. You might miss some errors or write repetitive code. When new errors happen, you have to add more checks everywhere, making your program messy and hard to fix.
Using an exception hierarchy groups related errors under common categories. You can catch broad groups of errors or specific ones easily. This keeps your code clean, organized, and ready for new error types without rewriting everything.
try: open('file.txt') except FileNotFoundError: print('File missing') except ConnectionError: print('No internet')
try: open('file.txt') except OSError: print('File or connection problem')
It enables writing simpler, clearer error handling that adapts easily as your program grows.
Think of a customer service center where calls are first sorted by department (billing, tech support) before being handled by specialists. Exception hierarchy sorts errors similarly, so your program knows where to look first.
Manual error handling is repetitive and fragile.
Exception hierarchy groups related errors for easier handling.
This leads to cleaner, more maintainable code.
Practice
Solution
Step 1: Understand Python's exception hierarchy
All exceptions in Python inherit fromBaseException, which is the root of the hierarchy.Step 2: Identify the base class
Exceptioninherits fromBaseException, butBaseExceptionis the top-level base class.Final Answer:
BaseException -> Option BQuick Check:
BaseException is the root of all exceptions [OK]
- Confusing Exception with BaseException
- Thinking Error is a built-in base class
- Choosing RuntimeError as base
Solution
Step 1: Recall exception hierarchy for catching exceptions
CatchingExceptioncatches most errors but excludes system-exiting exceptions likeSystemExitandKeyboardInterrupt.Step 2: Identify correct syntax
except Exception:is the standard way to catch all regular exceptions safely.Final Answer:
except Exception: -> Option CQuick Check:
Use Exception to catch all but system-exiting exceptions [OK]
- Using except BaseException catches system exit too
- Catching only RuntimeError misses many exceptions
- Using except SystemExit catches only exit exceptions
try:
x = 1 / 0
except ArithmeticError:
print('ArithmeticError caught')
except ZeroDivisionError:
print('ZeroDivisionError caught')Solution
Step 1: Understand exception hierarchy for ZeroDivisionError
ZeroDivisionErroris a subclass ofArithmeticError.Step 2: Check which except block matches first
SinceArithmeticErrorcomes beforeZeroDivisionError, the first except block catches the exception.Final Answer:
ArithmeticError caught -> Option AQuick Check:
Parent exception catches before child [OK]
- Expecting ZeroDivisionError block to run first
- Thinking exception order does not matter
- Assuming program crashes without handling
try:
open('file.txt')
except IOError:
print('File error')
except FileNotFoundError:
print('File not found')Solution
Step 1: Understand exception hierarchy between IOError and FileNotFoundError
FileNotFoundErroris a subclass ofIOError.Step 2: Check order of except blocks
The more specific exception (FileNotFoundError) must come before the more general (IOError) to avoid unreachable code.Final Answer:
FileNotFoundError should come before IOError -> Option DQuick Check:
Specific exceptions must precede general ones [OK]
- Putting general exceptions before specific ones
- Assuming IOError and FileNotFoundError are unrelated
- Thinking open() requires mode argument always
KeyboardInterrupt and SystemExit. Which is the best way to write the except block?Solution
Step 1: Recall exception hierarchy for KeyboardInterrupt and SystemExit
BothKeyboardInterruptandSystemExitinherit directly fromBaseException, notException.Step 2: Choose except block that excludes these exceptions
except Exception:catches all exceptions exceptKeyboardInterruptandSystemExit, which is the desired behavior.Final Answer:
except Exception: -> Option AQuick Check:
Exception excludes system-exiting exceptions [OK]
- Using except BaseException catches everything including system exit
- Catching KeyboardInterrupt and SystemExit explicitly when not needed
- Combining Exception with KeyboardInterrupt in except tuple
