PCAP Exceptions and File I/O Practice Question
A developer is writing a script that processes user-uploaded CSV files. The script should attempt to read the file, and if a UnicodeDecodeError occurs, log a warning and skip the file. Which code snippet correctly achieves this without stopping the entire process?
⚠ Common exam trap
Python Institute often tests the distinction between UnicodeDecodeError (input decoding) and UnicodeEncodeError (output encoding), and the trap here is that candidates confuse the two or use an overly broad except clause that hides bugs.
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
✓
try: read_file() except UnicodeDecodeError: log.warning('Skipping file')
It catches only UnicodeDecodeError, which is the specific exception raised when a file contains invalid UTF-8 or other encoding issues. It logs a warning and then continues execution (the 'pass' is implied by the log statement, but the key is that the exception is handled without re-raising). This matches the requirement to skip the file without stopping the entire process.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
try: read_file() except (UnicodeDecodeError, OSError): pass
Why it's wrong here
Catching a tuple of UnicodeDecodeError and OSError then silently passing is dangerous because it groups two unrelated failure modes: a file that cannot be decoded versus a file that cannot be read due to a missing path or permissions. Using pass discards the exception entirely, so there is no log record to indicate why a file was skipped, which is poor operational practice. Moreover, catching OSError here is unnecessary when the goal is logically to skip only corrupt encoded files, and it could hide real I/O problems.
- ✓
try: read_file() except UnicodeDecodeError: log.warning('Skipping file')
Why this is correct
This is the correct approach because it deliberately catches only UnicodeDecodeError, which is the exception raised when reading and decoding bytes containing invalid UTF-8 or another specified codec. By logging a warning and doing nothing else, the handler reports the problem while allowing the surrounding loop to move on to the next uploaded file. More specific handlers like this leave unrelated exceptions—such as OSError or file-system errors—free to propagate, preserving their diagnostic value.
- ✗
try: read_file() except Exception: log.warning('Skipping file')
Why it's wrong here
Using except Exception is far too broad for this use case because it intercepts every normal runtime exception, including programming mistakes like TypeError, KeyError, or ValueError, and masks them as 'skipped file' warnings. That means a genuine bug in read_file() would be hidden behind a misleading log message, making it nearly impossible to recognize and fix the actual error. Also, a blanket-except handler can interfere with cleanup or context-manager behaviors, and it captures errors that have nothing to do with decoding user uploads.
- ✗
try: read_file() except UnicodeEncodeError: log.warning('Skipping file')
Why it's wrong here
UnicodeEncodeError is raised when Unicode text is being converted into bytes—for example, during str.encode() or when writing to an output stream with a specific encoding—not when reading raw bytes and decoding them into text. Since read_file() in this context is reading a user-uploaded file, the failure point is decoding those bytes into a string, which raises UnicodeDecodeError instead. Although the two exceptions are siblings under UnicodeError, catching the encode variant would leave the actual decode failure completely unhandled, causing the program to crash.
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.