Error Handling Internals — Series 3

Preview — 3 of 10 questions

What is the output of the following code?

javascript
def risky():
    e = ValueError("bad input")
    e.add_note("check the config file")
    e.add_note("see docs at example.com")
    raise e

try:
    risky()
except ValueError as e:
    print(e.args)
    print(e.__notes__)
A('bad input',), ['check the config file', 'see docs at example.com']
B('bad input', 'check the config file', 'see docs at example.com'), []
C('bad input',), 'see docs at example.com' (only the most recent note survives)
DAttributeError: 'ValueError' object has no attribute 'add_note'

What is the output of the following code?

javascript
class AlwaysEqual(Exception):
    def __eq__(self, other):
        return True

class Other(Exception):
    pass

e1 = AlwaysEqual("a")
e2 = Other("b")
print(e1 == e2)

try:
    raise e2
except AlwaysEqual:
    print("caught by AlwaysEqual")
except Other:
    print("caught by Other")
ATrue, both except blocks run
BTrue, caught by Other
CTrue, caught by AlwaysEqual
DFalse, caught by Other

What happens when the following code runs?

javascript
class Noisy:
    def __del__(self):
        raise RuntimeError("boom in __del__")

def make():
    n = Noisy()
    del n

make()
print("still running")
AThe exception is completely silent — nothing is reported anywhere, and "still running" prints
BTypeError: __del__() should not raise an exception
Cstill running prints; the RuntimeError is reported separately (e.g. to stderr) but doesn't propagate or crash the program
DRuntimeError: boom in __del__ propagates uncaught, crashing before "still running" can print

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.