Exception Handling Flashcards
7 cards from real Python practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.
Read the first 7 Exception Handling flashcards as text
What is the purpose of the `raise ... from ...` syntax introduced in Python 3?
Answer: To chain exceptions and explicitly set the cause
`raise NewException() from original` explicitly chains exceptions, setting `__cause__` so the traceback shows the causal relationship.
Which attribute stores the exception that caused another exception when using implicit exception chaining?
Answer: __context__
`__context__` is set automatically when an exception is raised while another exception is already being handled (implicit chaining).
What does `raise ValueError('bad input') from None` do?
Answer: Suppresses the exception chain, hiding the original cause
Using `from None` suppresses the exception chain so the original exception context is not displayed in the traceback.
How can a custom exception class pass additional data to the handler?
Answer: By overriding __init__ to store extra attributes
Override `__init__` in your custom exception to accept and store extra attributes that handlers can access via the exception object.
What is the base class for all non-system-exiting exceptions in Python?
Answer: Exception
`Exception` is the base class for all regular (non-system-exiting) exceptions; catching it excludes `SystemExit`, `KeyboardInterrupt`, etc.
Which exception is NOT a subclass of `Exception`?
Answer: KeyboardInterrupt
`KeyboardInterrupt` inherits directly from `BaseException`, not `Exception`, so `except Exception` won't catch it.
What happens if no `except` clause matches a raised exception?
Answer: The exception propagates up the call stack
If no matching `except` clause is found, the exception propagates to the caller and continues up the call stack until caught or it terminates the program.