Exceptions & Files · Question 9 · Medium

An out-of-range list index is handled by two except clauses, the first naming a parent class. Which message is printed?

try:
    [1, 2][5]
except LookupError:
    print("lookup")
except IndexError:
    print("index")
  1. lookup
  2. index
  3. lookup index
  4. IndexError
Answer and explanation

Answer: A. lookup

Explanation: IndexError is a subclass of LookupError. except clauses are tested from top to bottom and only the first match runs, so the LookupError clause handles it.

Previous: A try statement has except, else and finally clauses, and an error occurs in… · Next: A custom exception carries a message built inside withdraw. What does printing…

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