PCAP Exceptions and File I/O Practice Question
A developer is writing a function that reads a configuration file. The function must distinguish between a missing file, a permission problem, and a malformed file, and it must not leave the file open on any path. Which TWO techniques are appropriate for this requirement? (Choose two.)
⚠ Common exam trap
The trap here is believing that CPython's reference counting makes explicit close() unnecessary, when deterministic cleanup should use with or finally.
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
✓
Catch FileNotFoundError and PermissionError in separate except clauses to produce distinct error messages.
Using with open(...) guarantees the file is closed on every exit path, and catching FileNotFoundError and PermissionError separately lets the function report distinct causes. The broad Exception handler hides bugs and relies on message text, the success-only close leaks the file on errors, and relying on the garbage collector is non-deterministic. Together, the context manager and specific handlers meet the stated requirements.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Catch Exception in a single handler and inspect the message text to decide which error occurred.
Why it's wrong here
Catching the broad Exception class hides unrelated bugs and forces fragile string matching on the message, which varies across platforms and Python versions. It also does not reliably distinguish a missing file from a permission problem, because both are OSError subclasses with different messages. This approach undermines the requirement to handle each condition distinctly and makes the function harder to maintain.
- ✗
Open the file without a with statement and call close() only after the parsing code completes successfully.
Why it's wrong here
Calling close() only on the success path leaves the file open when parsing raises an exception, which violates the requirement to close on every path. To be correct without with, the close would need to be in a finally block. Relying on the success path alone is exactly the resource-leak pattern the scenario is trying to avoid.
- ✓
Catch FileNotFoundError and PermissionError in separate except clauses to produce distinct error messages.
Why this is correct
FileNotFoundError and PermissionError are distinct subclasses of OSError, so separate except clauses allow the function to report a missing file differently from a permission problem. This satisfies the requirement to distinguish those two cases. A malformed file would still need separate handling, such as catching a parsing exception, but these two clauses cover the file-access distinctions the scenario calls out.
- ✓
Use a with open(...) as f: block so the file is closed whether the body succeeds or raises.
Why this is correct
The with statement creates a context manager whose __exit__ closes the file even if an exception occurs inside the block. This directly satisfies the requirement that the file must not be left open on any path. It also keeps the code concise and avoids the common bug of forgetting to call close() in a finally clause when multiple branches are involved.
- ✗
Rely on the garbage collector to close the file when the function returns, and raise a custom exception for all failures.
Why it's wrong here
CPython often closes a file when its reference count drops to zero, but this is an implementation detail and not a language guarantee; other implementations may delay it. Relying on the garbage collector can leave the file open longer than intended and is not a deterministic cleanup strategy. Raising one custom exception for all failures also fails the requirement to distinguish missing, permission, and malformed cases.
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.