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 happens when an exception is raised inside a `finally` block?
Answer: It replaces any exception from the try block
An exception raised in a `finally` block replaces any exception that was propagating from the `try` or `except` block.
Which built-in exception is raised when a key is not found in a dictionary?
Answer: KeyError
`KeyError` is raised when a dictionary lookup fails because the specified key does not exist.
How do you re-raise the currently-handled exception without losing the original traceback?
Answer: raise
A bare `raise` statement inside an `except` block re-raises the current exception while preserving its original traceback.
What is the output of: `print(1/0)` if wrapped with `except ZeroDivisionError as e: print(e)`?
Answer: division by zero
The `str()` representation of a `ZeroDivisionError` is 'division by zero', which is what `print(e)` outputs.
Which clause is executed only when no exception occurs in the `try` block?
Answer: else
The `else` clause runs only if the `try` block completes without raising any exception.
What does `except (TypeError, ValueError):` accomplish?
Answer: Catches either TypeError or ValueError
Grouping exception types in a tuple within a single `except` clause catches any of the listed exception types.
Which exception is raised when you try to access a list index that is out of range?
Answer: IndexError
`IndexError` is raised when a sequence subscript (index) is out of the valid range.