Exceptions & Files · Question 1 · Easy

A division by zero happens inside a try block that has a matching except clause. What is printed?

try:
    print(10 // 0)
except ZeroDivisionError:
    print("cannot divide")
  1. 0
  2. ZeroDivisionError
  3. 0 cannot divide
  4. cannot divide
Answer and explanation

Answer: D. cannot divide

Explanation: 10 // 0 raises ZeroDivisionError before print gets any value. The matching except block handles it and prints its message, and the program continues normally.

Next: Which exception does Python raise when int() is given text that is not a whole…

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