Exceptions & Files · Question 18 · Hard
parse() catches one exception and raises a different one from its handler. What does the outer handler print?
def parse(s):
try:
return int(s)
except ValueError:
raise TypeError("bad")
try:
parse("x")
except (ValueError, TypeError) as e:
print(type(e).__name__, e)
- ValueError bad
- ValueError invalid literal
- TypeError invalid literal
- TypeError bad
Answer and explanation
Answer: D. TypeError bad
Explanation: int("x") raises ValueError, which is caught inside parse. The handler then raises a new TypeError("bad"), and that is the exception that reaches the outer try, whose tuple of classes accepts it.
Previous: All three parts of this try statement contain a return. What does risky() give… · Next: Which of these exceptions is NOT caught by a handler written as except…
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
- readline() is called twice and then read() once on a three-line file-like…
- A function returns from inside try, and its finally block has a print. In what…
- All three parts of this try statement contain a return. What does risky() give…
- Which of these exceptions is NOT caught by a handler written as except…
- Two lines are written to an in-memory text file, which is read once before and…
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