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__)
  1. A. r ZeroDivisionError
  2. B. r RuntimeError
  3. C. ZeroDivisionError RuntimeError
  4. 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

Exams that ask this topic