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 a `SyntaxError` in Python?
Answer: An error detected by the parser when code violates Python grammar rules
`SyntaxError` is raised by the Python parser when the source code does not conform to Python's grammar, before execution begins.
What does the following snippet output? try: pass except Exception: print('error') else: print('ok')
Answer: ok
Since no exception is raised in the `try` block, the `else` clause executes and prints 'ok'.
Which exception type would be raised by `int(None)`?
Answer: TypeError
`int(None)` raises `TypeError` because `None` is not a valid type to convert to an integer.
How can you create a custom exception class in Python?
Answer: By defining a class that inherits from Exception
Custom exceptions are created by defining a class that inherits from `Exception` or one of its subclasses.
What is the output of this code? try: raise TypeError('bad type') except ValueError: print('V') except TypeError: print('T') except Exception: print('E')
Answer: T
Python matches exceptions to the first matching `except` clause; `TypeError` matches the second handler, so 'T' is printed.
Which built-in exception is raised when a function receives the wrong number of arguments?
Answer: TypeError
Calling a function with the wrong number of arguments raises `TypeError`, which covers type-related errors including argument mismatches.
What does `except Exception as e` store in `e`?
Answer: The exception instance
The `as e` clause binds the actual exception instance to `e`, allowing access to its message and attributes.