Which Python keyword is used to handle exceptions and guarantee execution of cleanup code regardless of whether an exception occurred?

Answer and explanation

Correct answer: B. finally

  1. Step 1: Review Python try-except-else-finally blocks.
  2. Step 2: The `finally` clause always executes before leaving the try statement, regardless of whether exceptions were raised or caught, making it the standard block for releasing resources (e.g., closing file streams).
Answer
finally.
Why not the others: `except` catches specific exceptions; `catch` is not a Python keyword (used in C++/Java); `else` executes only when no exceptions are raised.

Practise more Algorithms and Python Programming.

Free questions with an explanation for every one, and full timed EmSAT Achieve (UAE) mock tests.