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 happens when a `try` block raises an exception that has no matching `except` clause?
Answer: Python searches for a matching handler up the call stack
If no matching `except` clause is found in the current `try` block, Python propagates the exception up the call stack looking for a handler.
Which clause in a try/except structure always executes, whether or not an exception occurred?
Answer: finally
The `finally` clause always executes regardless of whether an exception was raised or caught.
What does the following code print? try: x = 1 / 0 except ZeroDivisionError: print('A') else: print('B') finally: print('C')
Answer: A C
The `except` block prints 'A' because a ZeroDivisionError is raised, `else` is skipped, and `finally` always prints 'C'.
Which built-in exception is raised when an operation receives the correct type but an inappropriate value?
Answer: ValueError
`ValueError` is raised when a function receives an argument of the correct type but an invalid value, such as `int('abc')`.
What is the output of this code? try: print(1) raise ValueError print(2) except ValueError: print(3) print(4)
Answer: 1 3 4
Python prints 1, then the ValueError is raised skipping print(2), the except block prints 3, and execution continues with print(4).
How do you access the exception object inside an `except` clause?
Answer: except Exception as e:
The syntax `except ExceptionType as e:` binds the exception instance to the variable `e` for use within the handler.
Which exception is raised when you try to access a dictionary key that does not exist?
Answer: KeyError
`KeyError` is raised when a dictionary is accessed with a key that is not present in it.