Courseiva

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

A developer is writing a robust script that must handle file reading errors. The script should catch only I/O-related exceptions (e.g., FileNotFoundError, PermissionError) and let other exceptions propagate. Which exception handling structure is best suited?

⚠ Common exam trap

The PCEP exam often tests the misconception that a single `except Exception:` is sufficient for selective handling, but candidates fail to realize it catches all `Exception` subclasses, including non-I/O ones, thus violating the requirement to let other exceptions propagate.

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

✓

Use multiple except blocks for specific exception types

Using multiple `except` blocks for specific exception types (e.g., `FileNotFoundError`, `PermissionError`) allows the script to catch only I/O-related exceptions while letting all other exceptions propagate unhandled. This matches the requirement precisely, as each `except` clause targets a distinct exception class, and any unlisted exception will not be caught, preserving the intended error propagation behavior.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    Use a single bare except: clause

    Why it's wrong here

    A bare except catches every exception, including the non-I/O ones the script must let propagate, so it violates the stated requirement. It tempts because it is the quickest way to stop crashes, and it would be correct only when every possible failure should be handled identically.

  • ✗

    Use except: and then re-raise the exception

    Why it's wrong here

    A bare except catches everything, including KeyboardInterrupt and SystemExit, then re-raises, so no exception is actually filtered; the I/O-only requirement is unmet. It is tempting as a logging wrapper, and would be correct where every exception must be recorded before propagating unchanged.

  • ✓

    Use multiple except blocks for specific exception types

    Why this is correct

    Catching FileNotFoundError and PermissionError in separate except blocks lets the script handle each I/O failure precisely while unhandled exceptions, such as ValueError, propagate naturally. This satisfies the stem's requirement to catch only I/O-related exceptions, since a single broad handler would incorrectly intercept unrelated errors.

  • ✗

    Use except Exception: as the only handler

    Why it's wrong here

    Catching Exception intercepts every non-system-exiting error, including TypeError and ValueError, so unrelated faults are swallowed instead of propagating. It is tempting because a bare broad handler is quick to write, and it would suit a top-level safety net that logs and terminates, not selective I/O handling.

About these practice questions

This PCEP question is part of Courseiva's 482-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 →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCEP 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 PCEP exam.