Exceptions & Files · Question 14 · Medium

The only except clause below names TypeError, but the conversion fails in a different way. What is the result of running the code?

try:
    value = int("ten")
except TypeError:
    print("T")
  1. ValueError
  2. T
  3. TypeError
  4. Nothing is printed and the program ends normally
Answer and explanation

Answer: A. ValueError

Explanation: int("ten") raises ValueError, and ValueError is not a subclass of TypeError. No clause matches, so the exception is not handled and the program stops with ValueError.

Previous: Why is with open("data.txt") as f: preferred over calling open() and close()… · Next: readline() is called twice and then read() once on a three-line file-like…

Practise exceptions & files

Try a fresh set of exceptions & files questions with an explanation after every answer, free and without signing up: Exceptions & Files practice. Or sit a full placement mock test.

More exceptions & files questions

More python practice

Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope · Comprehensions & Generators

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge