Error Handling and Debugging Flashcards
7 cards from real PCEP practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Error Handling and Debugging flashcards as text
What is the base class for all built-in exceptions in Python?
Answer: BaseException
`BaseException` is the base class for all built-in exceptions, while `Exception` is a subclass of `BaseException` used for most non-system-exiting exceptions.
What does `raise` without any arguments do inside an `except` block?
Answer: Re-raises the currently handled exception
A bare `raise` statement inside an `except` block re-raises the exception currently being handled.
Which of the following correctly catches multiple exception types in a single except clause?
Answer: except (ValueError, TypeError):
Multiple exception types are caught by placing them in a tuple: `except (ValueError, TypeError):`.
What exception is raised when a variable is referenced before it has been assigned a value?
Answer: NameError
`NameError` is raised when a local or global name cannot be found, which includes using a variable before assigning it.
What is the result of this code? def f(): try: return 1 finally: return 2 print(f())
Answer: 2
When both `try` and `finally` contain `return`, the `finally` block's `return` overrides the `try` block's `return`.
Which exception is raised when a `list.index()` call cannot find the specified value?
Answer: ValueError
`list.index()` raises `ValueError` when the specified value is not present in the list.
What is the purpose of the `assert` statement in Python?
Answer: To test a condition and raise AssertionError if it is False
`assert condition, message` raises `AssertionError` with the optional message if the condition evaluates to False.