ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
class E(Exception):
    def __str__(self): return "custom"
try:
    try:
        raise E("orig")
    except E as e:
        raise TypeError("wrap") from e
except TypeError as t:
    c = t.__cause__
    print(type(c).__name__, str(c), c.args, t.__context__ is c)
  1. A. E orig ('orig',) True
  2. B. E custom ('orig',) False
  3. C. TypeError wrap ('wrap',) True
  4. D. E custom ('orig',) True

Correct answer: D. E custom ('orig',) True

`raise ... from e` sets __cause__ to e; because it is raised inside the handler, __context__ is also e. The overridden __str__ controls str(), while args still holds the constructor argument.

More Python Programming questions

Exams that ask this topic