Exceptions & Files · Question 3 · Easy

In a try statement, when does the code inside the finally block run?

  1. Always, whether or not an exception was raised in the try block
  2. Only when the try block raises an exception
  3. Only when the try block finishes without an exception
  4. Only when no except clause matches the exception
Answer and explanation

Answer: A. Always, whether or not an exception was raised in the try block

Explanation: finally is meant for clean-up such as closing a file, so Python runs it on every path out of the try statement: normal completion, a handled exception or an unhandled one.

Previous: Which exception does Python raise when int() is given text that is not a whole… · Next: A dictionary of marks is asked for a name that was never stored. Which…

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