Courseiva
Exceptions and File I/O →hardMultiple Choice

PCAP Exceptions and File I/O Practice Question

A Python application processes user-uploaded files. The requirement is to catch any I/O-related exception while reading the file, but not to catch KeyboardInterrupt or SystemExit. Which exception type should be caught?

⚠ Common exam trap

Python Institute often tests the Python 3 exception hierarchy change where `IOError` is no longer a separate class but an alias of `OSError`, tempting candidates to pick the familiar `IOError` from Python 2 instead of the correct `OSError`.

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

✓

OSError

`OSError` is the base class for all I/O-related exceptions in Python 3, including file reading errors like `FileNotFoundError` and `PermissionError`. It does not catch `KeyboardInterrupt` or `SystemExit`, which inherit directly from `BaseException`, not `Exception`. This makes `OSError` the precise choice for catching I/O errors while allowing program termination signals to propagate.

Answer analysis

Option-by-option breakdown

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

  • ✓

    OSError

    Why this is correct

    OSError is the correct choice because it is the built-in exception class for all operating-system-related I/O failures—file not found, permission denied, disk full, or invalid file descriptor—and it deliberately excludes system-exiting exceptions like KeyboardInterrupt and SystemExit. Catching OSError lets the application handle upload failures gracefully without masking fatal interpreter or user-interrupt conditions. Since Python 3.3, IOError is an alias of OSError, so OSError is the canonical, forward-compatible name to use.

  • ✗

    IOError

    Why it's wrong here

    IOError is the historical name for I/O errors, but in Python 3 it has been merged into OSError as an alias—it is not a separate class, and relying on it can mislead readers or fail in code that imports the name under Python 2-style expectations. Catching IOError is effectively identical to catching OSError, but since the alias is deprecated in spirit and can be confusing, it is not the preferred way to express the exception. The correct approach is to name OSError directly.

  • ✗

    Exception

    Why it's wrong here

    Catching Exception is too broad for a file-processing context because it catches every built-in exception that does not inherit from BaseException, including ValueError, TypeError, and other programming errors unrelated to I/O. This can hide bugs in the processing logic or in the upload framework, making failures appear to be file-system issues when they are actually coding mistakes. Additionally, Exception does not cover KeyboardInterrupt or SystemExit, but it still masks far more than the specific OSError conditions the application should respond to.

  • ✗

    BaseException

    Why it's wrong here

    BaseException is the root of Python's exception hierarchy, so catching it also traps SystemExit, KeyboardInterrupt, and GeneratorExit—control-flow signals that should normally terminate or interrupt the program, not be swallowed as routine upload errors. A file-processing app that catches BaseException will prevent the user from pressing Ctrl+C to cancel a stuck operation, and will intercept shutdown requests, leaving the process in an unpredictable state. Only OSError (or a narrower subclass) is appropriate when the goal is to handle file I/O failures.

About these practice questions

One of 421 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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 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.