PCAP Exceptions and File I/O Practice Question
A developer writes a function that reads a file and processes its content. The function should handle the case where the file does not exist without catching other I/O errors. Which exception should be caught?
⚠ Common exam trap
Python Institute often tests the Python exception hierarchy, and the trap here is that candidates mistakenly choose `IOError` or `OSError` because they are broader and seem 'safer,' but the question explicitly requires handling only the missing-file case without catching other I/O errors.
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 specific subclass of `OSError` that is raised exactly when a file or directory is requested but does not exist. By catching only `FileNotFoundError`, the function handles the missing-file scenario without masking other I/O errors such as permission issues or disk failures, which is the precise requirement stated in the question.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
PermissionError
Why it's wrong here
PermissionError is a built-in exception that signals insufficient access rights to an OS resource—for example, when a file exists but the current user's mode denies reading it. Since the missing-file condition produces errno ENOENT rather than EACCES or EPERM, PermissionError never fires when the file simply is not present. Therefore it is not the appropriate exception to catch or expect for this scenario, even though permission checks happen before file access.
- ✗
IOError
Why it's wrong here
IOError in Python 3 is just an alias of OSError, and the name is deprecated from the Python 2 era where it represented every generic input/output problem, including device failures, disk-full conditions, and network errors. If the developer used it to handle a missing file, it would both be stylistically outdated and far too broad, potentially swallowing unrelated I/O errors. A precise handler should target FileNotFoundError instead, avoiding the old alias and relying on the modern exception hierarchy.
- ✗
OSError
Why it's wrong here
OSError is the common parent class of many filesystem and OS exceptions, including FileNotFoundError, PermissionError, IsADirectoryError, and NotADirectoryError. While catching OSError would technically intercept a missing-file situation, it would also catch every other OS-level failure, making the except block impossible to tailor—there would be no way to know whether the file was missing or some other kernel call failed. This lack of specificity makes OSError a poor choice when the intent is to recover specifically from a nonexistent file path.
- ✓
FileNotFoundError
Why this is correct
FileNotFoundError is the specific built-in exception that Python raises when an attempt to open or access a file path cannot succeed because the path does not exist, typically with errno ENOENT. For a read operation, open(path, 'r') will immediately raise it if the file is not present before any other processing occurs. Because it is narrowly scoped, catching FileNotFoundError lets the developer provide exactly the right fallback—like creating the file or printing a meaningful message—without masking permission problems or unrelated OS failures.
Go deeper
Related to this question
About these practice questions
This PCAP question is part of Courseiva's 169-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.