ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
def f():
try:
raise KeyError("k")
finally:
return "swallowed"
try:
print(f(), end=" ")
except KeyError:
print("KeyError", end=" ")
print("end")- A. swallowed end
- B. KeyError end
- C. swallowed KeyError end
- D. end
Correct answer: A. swallowed end
A return inside finally cancels the exception that is propagating, so f() returns normally and the KeyError is silently lost.
More Python Programming questions
- What is printed? python try: try: 1 / 0 except ZeroDivisionError: raise ValueError("v") except ValueError as e…
- 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…