All quizzesHard
Error Handling Internals — Series 2
Preview — 3 of 10 questions
What is the output of the following code?
javascript
class AlwaysTrue:
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, tb):
print("exit called, exc_type =", exc_type)
return True
with AlwaysTrue():
print("no exception here")
print("after block")Ano exception here, exit called, exc_type = None, then the block re-executes indefinitely, since __exit__ returned True.
Bexit called, exc_type = None, no exception here, after block
Cno exception here, after block — __exit__ is only called when an exception actually occurs.
Dno exception here, exit called, exc_type = None, after block
What is the output of the following code?
javascript
import sys
original_hook = sys.excepthook
def custom_hook(exc_type, exc_value, tb):
print(f"custom handler: {exc_type.__name__}: {exc_value}")
sys.excepthook = custom_hook
print(sys.excepthook is custom_hook)
sys.excepthook = original_hook
print(sys.excepthook is original_hook)ATrue, True
BFalse, True — sys.excepthook cannot be reassigned to an arbitrary function; only specific pre-registered callables are accepted.
CTypeError: excepthook is read-only
DTrue, False — restoring sys.excepthook to its original value afterward silently fails.
What is the output of the following code?
javascript
from contextlib import ExitStack
def setup():
stack = ExitStack()
stack.callback(lambda: print("cleanup A"))
stack.callback(lambda: print("cleanup B"))
return stack.pop_all()
transferred = setup()
print("using resources")
transferred.close()Acleanup A, cleanup B, using resources — pop_all() immediately triggers all registered cleanups before returning.
Busing resources, cleanup B, cleanup A
Cusing resources only — pop_all() silently discards every registered callback instead of transferring them.
Dusing resources, cleanup A, cleanup B
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.