PCAP Exceptions and File I/O Practice Question
A programmer wants to catch both `FileNotFoundError` and `PermissionError` with a single except clause. Which tuple is correct?
⚠ Common exam trap
Python Institute often tests the distinction between the correct tuple syntax `except (Exc1, Exc2):` and the incorrect comma-separated syntax `except Exc1, Exc2:` (which is a Python 2 relic), or the overly broad `except OSError:` that catches more than intended.
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
✓
except (FileNotFoundError, PermissionError):
Python's exception handling syntax allows a tuple of exception types in a single `except` clause, enabling the programmer to catch multiple exception types with the same handler. Both `FileNotFoundError` and `PermissionError` are subclasses of `OSError`, but using the tuple explicitly catches only those two specific exceptions, not all `OSError` subtypes.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
except (FileNotFoundError, PermissionError):
Why this is correct
Correct syntax: parentheses form a tuple listing the two exception classes to catch. Python checks the raised exception against each class in the tuple and handles either FileNotFoundError or PermissionError in the same block. Because both derive from OSError, using this tuple is more precise than catching the parent class and avoids masking unrelated OSError subclasses.
- ✗
except FileNotFoundError, PermissionError:
Why it's wrong here
Invalid syntax: this comma-separated form was inherited from Python 2, where `except E, e` assigned the exception instance to `e`. Python 3 removed that syntax; you must use `except E as e` or supply a tuple. Without parentheses, Python parses the comma as a tuple construction and raises a SyntaxError, so the line will not even compile.
- ✗
except OSError:
Why it's wrong here
Catching `OSError` is overly broad: it will intercept every subclass of OSError, including FileNotFoundError and PermissionError, but also IsADirectoryError, NotADirectoryError, TimeoutError, and other low-level I/O errors. This can hide programming bugs and make error handling ambiguous. To handle only the two intended errors, catch the exact classes, not the parent class.
- ✗
except [FileNotFoundError, PermissionError]:
Why it's wrong here
Using a list literal for the exception targets is not permitted. The `except` clause requires an exception class or a tuple of classes; a list is not hashable and does not inherit from BaseException. The interpreter will raise `TypeError` when an error occurs because it cannot match against a list object, so this code will not work at runtime.
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.