PCAP Exceptions and File I/O Practice Question
A developer writes a helper that copies lines from one text file to another. The helper opens both files with open() inside a try block and closes them in a finally block. If an exception occurs while writing, which statement about the finally block is correct?
⚠ Common exam trap
The trap here is thinking that finally is conditional on an except clause catching the exception, when finally runs unconditionally during unwinding.
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
✓
The finally block runs before the exception propagates, so both files can be closed there.
A finally clause executes during the unwinding of its try statement, before the exception propagates to the caller. That makes it the right place to close both files when the write fails, ensuring cleanup without catching or suppressing the error. The other statements misdescribe when finally runs relative to exception handling and propagation.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
The finally block runs only if the exception is caught by an except clause in the same try statement.
Why it's wrong here
finally runs regardless of whether an except clause catches the exception. If no handler matches, the exception still triggers the finally block before propagating. Tying finally execution to the presence of a matching except clause is incorrect and would cause developers to add unnecessary handlers just to force cleanup, which is not how the construct works.
- ✗
The finally block is skipped when an exception occurs, so the files remain open.
Why it's wrong here
The opposite is true: finally is specifically designed to run during exception unwinding. Skipping it would happen only if the process were terminated abruptly, such as by os._exit() or a crash, which is not the scenario described. Claiming that finally is skipped on exceptions misunderstands the purpose of the clause and would lead to unnecessary workarounds.
- ✗
The finally block runs after the exception has propagated to the caller, so closing there is too late.
Why it's wrong here
The finally block runs during unwinding of the try statement, before the exception reaches the caller. By the time the caller sees the exception, cleanup has already happened. Describing finally as running after propagation is backwards and would incorrectly suggest that resource cleanup cannot be guaranteed with this pattern.
- ✓
The finally block runs before the exception propagates, so both files can be closed there.
Why this is correct
A finally block executes as the try statement unwinds, whether the try block completed normally or raised. In this scenario, when the write fails, control transfers to finally before the exception continues to the caller, so both file handles can be closed there. This guarantees cleanup without suppressing the original exception, which is the intended behavior.
Go deeper
Related to this question
About these practice questions
One of 421 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official Python Institute exam blueprint
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.