ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
try:
try:
{}["x"]
except KeyError as k:
raise RuntimeError("r") from None
except RuntimeError as e:
print(type(e.__context__).__name__, e.__cause__, e.__suppress_context__)- A. NoneType None True
- B. KeyError None True
- C. KeyError None False
- D. NoneType None False
Correct answer: B. KeyError None True
`raise X from None` sets __cause__ = None and __suppress_context__ = True, which hides the KeyError in tracebacks; but __context__ is still recorded as the KeyError.
More Python Programming questions
- What is printed? python out = [] for x in [1, 0, "a"]: try: r = 10 / x except (ZeroDivisionError, TypeError) as e…
- What is printed? python class AppError(Exception): pass class DBError(AppError): pass try: raise DBError("db") except Exception…
- What is printed? python def f(): try: raise ValueError("a") except ValueError as e: err = e return err x = f() print(type(x).name, x.args…
- What is printed? python try: raise ExceptionGroup("g", [ValueError(1), TypeError(2), ValueError(3)]) except ValueError as eg…
- What is printed? python def f(n): try: if n: raise IndexError return "body" except IndexError: return "except" else: return "else"…
- What is printed? python import math x = float("inf") print(x - x, x 0 == x 0, math.isnan(x / x), -x < 0 < x, 1e308 10 == x)