ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
try:
    raise TypeError("t")
except (ValueError, TypeError) as e:
    print("caught", type(e).__name__)
  1. A. caught ValueError
  2. B. caught TypeError
  3. C. caught Exception
  4. D. TypeError traceback

Correct answer: B. caught TypeError

A tuple of exception types in except matches ANY of them; since TypeError is raised and TypeError is in the tuple, it is caught, and the actual runtime type (TypeError) is reported.

More Python Programming questions

Exams that ask this topic