ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
try:
try:
1 / 0
except ZeroDivisionError:
raise ValueError("v")
except ValueError as e:
print(type(e.__context__).__name__, e.__cause__, e.__suppress_context__)- A. ZeroDivisionError ZeroDivisionError True
- B. None None False
- C. ZeroDivisionError None False
- D. ZeroDivisionError None True
Correct answer: C. ZeroDivisionError None False
Raising inside an except block implicitly chains: __context__ is the ZeroDivisionError. Without `raise ... from`, __cause__ stays None and __suppress_context__ is False.
More Python Programming questions
- What is printed? python try: try: {}["x"] except KeyError as k: raise RuntimeError("r") from None except RuntimeError as e…
- 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"…