Exceptions & Files · Question 17 · Hard

All three parts of this try statement contain a return. What does risky() give back?

def risky():
    try:
        return 10 // 0
    except ZeroDivisionError:
        return "except"
    finally:
        return "finally"

print(risky())
  1. except
  2. finally
  3. ZeroDivisionError
  4. except finally
Answer and explanation

Answer: B. finally

Explanation: The division raises, so the except block prepares to return "except". finally still runs before the function exits, and a return inside finally replaces any earlier pending return value.

Previous: A function returns from inside try, and its finally block has a print. In what… · Next: parse() catches one exception and raises a different one from its handler…

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