Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the base class for all exceptions in Python?
The base class for all exceptions in Python is <code>BaseException</code>. All other exceptions inherit from it.
Click to reveal answer
beginner
Which class do most user-defined exceptions inherit from?
Most user-defined exceptions inherit from the <code>Exception</code> class, which is a subclass of <code>BaseException</code>.
Click to reveal answer
intermediate
What is the difference between Exception and BaseException?
<code>BaseException</code> is the root of the exception hierarchy. <code>Exception</code> is a subclass used for most errors that programs should catch. Some exceptions like <code>SystemExit</code> and <code>KeyboardInterrupt</code> inherit directly from <code>BaseException</code> and are usually not caught by normal code.
Click to reveal answer
intermediate
Name two exceptions that inherit directly from BaseException and are usually not caught by normal exception handlers.
Two exceptions that inherit directly from BaseException are SystemExit and KeyboardInterrupt. They are used to exit programs or interrupt execution and are usually not caught by normal except Exception blocks.
Click to reveal answer
beginner
How does the exception hierarchy help in writing exception handling code?
The exception hierarchy allows you to catch broad categories of errors by catching a parent class like <code>Exception</code>, or catch specific errors by catching subclasses. This helps write flexible and clear error handling.
Click to reveal answer
Which class is the root of all exceptions in Python?
AError
BException
CStandardError
DBaseException
✗ Incorrect
BaseException is the root class for all exceptions in Python.
Which exception is NOT a subclass of Exception?
AValueError
BIndexError
CKeyboardInterrupt
DTypeError
✗ Incorrect
KeyboardInterrupt inherits directly from BaseException, not Exception.
If you want to catch most errors in your program, which class should you catch?
AException
BKeyboardInterrupt
CSystemExit
DBaseException
✗ Incorrect
Catching Exception will catch most program errors but not system-exiting exceptions.
Which exception is raised when a program is asked to exit?
ASystemExit
BRuntimeError
CValueError
DException
✗ Incorrect
SystemExit is raised when the program is asked to exit.
Why should you avoid catching BaseException directly?
AIt does not catch any exceptions.
BIt catches all exceptions including system-exiting ones, which can cause problems.
CIt only catches syntax errors.
DIt is deprecated.
✗ Incorrect
Catching BaseException catches all exceptions including SystemExit and KeyboardInterrupt, which can interfere with program exit or interruption.
Explain the Python exception hierarchy starting from BaseException.
Think about which exceptions are caught normally and which are special.
You got /4 concepts.
Why is it important to understand the exception hierarchy when writing try-except blocks?
Consider how catching a parent class affects what errors you handle.
You got /4 concepts.
Practice
(1/5)
1. Which of the following is the base class for all built-in exceptions in Python?
easy
A. Exception
B. BaseException
C. Error
D. RuntimeError
Solution
Step 1: Understand Python's exception hierarchy
All exceptions in Python inherit from BaseException, which is the root of the hierarchy.
Step 2: Identify the base class
Exception inherits from BaseException, but BaseException is the top-level base class.
Final Answer:
BaseException -> Option B
Quick Check:
BaseException is the root of all exceptions [OK]
Hint: Remember: BaseException is the root of all exceptions [OK]
Common Mistakes:
Confusing Exception with BaseException
Thinking Error is a built-in base class
Choosing RuntimeError as base
2. Which of the following is the correct syntax to catch all exceptions except system-exiting ones?
easy
A. except SystemExit:
B. except BaseException:
C. except Exception:
D. except RuntimeError:
Solution
Step 1: Recall exception hierarchy for catching exceptions
Catching Exception catches most errors but excludes system-exiting exceptions like SystemExit and KeyboardInterrupt.
Step 2: Identify correct syntax
except Exception: is the standard way to catch all regular exceptions safely.
Final Answer:
except Exception: -> Option C
Quick Check:
Use Exception to catch all but system-exiting exceptions [OK]
Hint: Use except Exception to avoid catching system-exit errors [OK]
Common Mistakes:
Using except BaseException catches system exit too
Catching only RuntimeError misses many exceptions
Using except SystemExit catches only exit exceptions