Exceptions & Files · Question 16 · Hard

A function returns from inside try, and its finally block has a print. In what order do the two words appear?

def check():
    try:
        return "try"
    finally:
        print("finally")

print(check())
  1. try finally
  2. try
  3. finally
  4. finally try
Answer and explanation

Answer: D. finally try

Explanation: The return value "try" is worked out first, but before the function actually exits, the finally block runs and prints finally. Only then does the caller receive "try" and print it.

Previous: readline() is called twice and then read() once on a three-line file-like… · Next: All three parts of this try statement contain a return. What does risky() give…

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