Exceptions & Files · Question 8 · Medium

A try statement has except, else and finally clauses, and an error occurs in the middle of the try block. Which letters are printed?

try:
    print("A")
    x = 1 / 0
    print("B")
except ZeroDivisionError:
    print("C")
else:
    print("D")
finally:
    print("E")
  1. A B C E
  2. A C D E
  3. C E
  4. A C E
Answer and explanation

Answer: D. A C E

Explanation: A is printed, then 1 / 0 raises, so B is skipped and control jumps to the except block (C). else runs only when there was no exception, and finally always runs (E).

Previous: io.StringIO lets a string be read like a text file. How many lines does… · Next: An out-of-range list index is handled by two except clauses, the first naming…

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