ProgrammingHardGATE CS · Product-company placement
A service catches errors with `except Exception:` around its main loop. Which of the following will NOT be caught by that handler?
- A. ZeroDivisionError from a bad calculation
- B. StopIteration raised manually inside the loop body
- C. UnicodeDecodeError while reading a file
- D. KeyboardInterrupt raised when the user presses Ctrl+C
Correct answer: D. KeyboardInterrupt raised when the user presses Ctrl+C
KeyboardInterrupt, SystemExit and GeneratorExit derive from BaseException, not Exception, precisely so that a broad `except Exception` does not swallow them. UnicodeDecodeError is a ValueError subclass, and StopIteration is an ordinary Exception.
More Python Programming questions
- For a Python list a with n elements, which operation is O(n) in the worst case (not amortised O(1))?
- How many of these statements about CPython are correct? 1. A tuple is hashable only if all its elements are hashable. 2. x in someset is…
- In CPython, two threads each run a pure-Python CPU-bound loop (no I/O, no C extensions releasing the lock) on a 4-core machine. Compared…
- Which statement about iterables and iterators is correct?
- Assertion (A): After t = (1, 2, [3]), the statement t[2].append(4) succeeds and changes what print(t) shows. Reason (R): A tuple's…
- Which of these expressions raises an exception in Python 3?