Python finally block always executes — Behavior and Guarantees
Which of the following statements about the `finally` block is true?
Quick Answer
The correct answer is that the `finally` block always executes, regardless of exceptions. This guarantee holds because Python’s exception handling model is designed to run the `finally` clause after the `try` and `except` blocks complete, no matter what—whether an exception is raised, caught, or even if a `return`, `break`, or `continue` statement is encountered inside the `try` block. On the Certified Associate Python Programmer PCAP exam, this concept tests your understanding of cleanup guarantees and resource management, often appearing in questions that present tricky scenarios like a `return` inside `try` followed by a `finally` block. A common trap is assuming `finally` won’t run if an unhandled exception occurs, but in reality it executes before the exception propagates. Remember the mnemonic: “Finally is final—it always fires, no exceptions.”
⚠ Common exam trap
Python Institute often tests the misconception that a `return` statement in the `try` block prevents the `finally` block from executing, but in Python, the `finally` block always runs before the function returns, making this a common trap for candidates who confuse Python's behavior with that of other languages.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
It always executes, regardless of exceptions.
The `finally` block in Python is designed to always execute after the `try` and `except` blocks, regardless of whether an exception was raised or not. This includes cases where a `return`, `break`, or `continue` statement is executed in the `try` block, or even if an unhandled exception occurs. The `finally` block is guaranteed to run before the function returns or the exception propagates, ensuring cleanup actions like closing files or releasing resources.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
It executes only if no exception is raised.
Why it's wrong here
Finally always executes.
- ✗
It does not execute if a return statement is in try block.
Why it's wrong here
Finally executes even after return.
- ✗
It executes only if an exception is raised.
Why it's wrong here
Finally always executes.
- ✓
It always executes, regardless of exceptions.
Why this is correct
Finally is guaranteed to run.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCAP question from scratch — 169 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
Same concept, more angles
1 more way this is tested on PCAP
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. A developer wants to ensure that a file is always closed, even if an exception occurs, without using the 'with' statement. Which approach correctly achieves this?
medium- A.f = open('file.txt'); try: ... except: ... else: f.close()
- ✓ B.f = open('file.txt'); try: ... finally: f.close()
- C.if f.closed: pass else: f.close()
- D.try: f = open('file.txt'); ... except: pass; finally: f.close()
Why B: The `finally` block is guaranteed to execute regardless of whether an exception occurs in the `try` block. This ensures that `f.close()` is always called, properly releasing the file resource. The `with` statement is not used, but the `try...finally` construct provides the same deterministic cleanup behavior.
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.