PCAP Exceptions and File I/O Practice Question
A developer writes a function that reads a configuration file and returns its contents as a string. The file might not exist. Which exception should be caught to handle a missing file?
⚠ Common exam trap
Python Institute often tests the distinction between the broad `OSError` and its specific subclass `FileNotFoundError`, trapping candidates who think catching the parent class is safer, when in fact the exam expects precise exception handling for a missing file.
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
✓
FileNotFoundError
`FileNotFoundError` is a built-in exception in Python that is raised when a file or directory is requested but does not exist. In Python 3, file-related I/O errors are organized under `OSError` with specific subclasses, and `FileNotFoundError` is the precise exception for a missing file, making it the most appropriate catch for this scenario.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
FileNotFoundError
Why this is correct
FileNotFoundError is a built-in exception raised when a file or directory is requested but cannot be found at the specified path. It is a subclass of OSError and is specifically designed for missing-file scenarios, which is exactly the case when reading a configuration file that does not exist. In Python 3, it is the canonical exception for this situation, replacing the more generic IOError that was used in earlier versions.
- ✗
PermissionError
Why it's wrong here
PermissionError is also a subclass of OSError, but it is raised when the operating system denies access to a resource due to insufficient permissions, such as read, write, or execute rights. In the scenario of a missing configuration file, the file is absent rather than being blocked by permissions, so the failure occurs earlier in the lookup process and does not involve access control. Choosing PermissionError would misidentify the root cause and complicate debugging.
- ✗
IOError
Why it's wrong here
IOError is an alias for OSError in Python 3; it is a broad category that covers many I/O failures, including missing files, permission issues, and device errors. While a missing configuration file would technically trigger an OSError, IOError is too generic and does not convey the specific reason—the file's absence—that a developer needs to handle. Relying on IOError forces the error handler to parse the error code or message to distinguish between failure modes, which is unnecessary when FileNotFoundError exists.
- ✗
OSError
Why it's wrong here
OSError is the parent class for most operating-system-related errors, including FileNotFoundError, PermissionError, and many others. Catching OSError for a missing configuration file is overly broad and would also catch unrelated errors like disk full or connection reset, masking the actual problem. FileNotFoundError is a more specific subclass, allowing precise handling and clearer error reporting, which is why OSError alone is inappropriate.
Go deeper
Related to this question
About these practice questions
This PCAP question is part of Courseiva's 421-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
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.