ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
try:
try:
1 / 0
except ZeroDivisionError:
raise RuntimeError("r")
except RuntimeError as e:
print(e, type(e.__context__).__name__)- A. r ZeroDivisionError
- B. r RuntimeError
- C. ZeroDivisionError RuntimeError
- D. r None
Correct answer: A. r ZeroDivisionError
Raising a new exception INSIDE an except block automatically sets its __context__ to the exception being handled (implicit chaining), even without 'from'. So e.__context__ is the ZeroDivisionError instance.
More Python Programming questions
- What is printed? python try: raise ValueError("x") from None except ValueError as e: print(e.cause, e.suppresscontext)
- What is printed? python try: try: raise KeyError("k") except ValueError: print("vl") except KeyError: print("kl")
- What is printed? python x = 5 print(f"{x=}")
- What is printed? python print(f"{'hi':>10}|") print(f"{'hi':<10}|") print(f"{'hi':^10}|")
- What is printed? python print(f"{10 / 3:.3f}") print(f"{10 // 3}") print(f"{2 10:,}")
- What is printed? python x = [] print(bool(x), bool([0]), bool({}), bool({0: 0}))