Courseiva
Exceptions and File I/OhardMultiple ChoiceObjective-mapped

PCAP Exceptions and File I/O Practice Question

A developer is creating a custom exception hierarchy for a library. The base exception is `LibraryError`. Which definition ensures that subclasses can be caught using the parent exception, but also allows distinguishing between different error types?

⚠ Common exam trap

Python Institute often tests the distinction between `Exception` and `BaseException`, and the trap here is that candidates mistakenly think any class named 'Error' is automatically an exception, or they choose Option B thinking `BaseException` is the correct base for all custom exceptions.

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

class LibraryError(Exception): pass class FileError(LibraryError): pass class ParseError(LibraryError): pass

It defines `LibraryError` as a subclass of `Exception`, which is the proper base class for all user-defined exceptions in Python. Subclasses `FileError` and `ParseError` inherit from `LibraryError`, so they can be caught with `except LibraryError` while still being distinguishable by their own type. This follows the standard Python exception hierarchy, where custom exceptions should derive from `Exception`, not `BaseException` or no base class.

Answer analysis

Option-by-option breakdown

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

  • class LibraryError: pass class FileError(LibraryError): pass class ParseError(LibraryError): pass

    Why it's wrong here

    Because LibraryError is a plain class that does not inherit from Exception or BaseException, the interpreter will not allow it to be used with raise. Even if an instance is created, an except Exception clause cannot catch it, and any except clause that names it will fail because it is not an exception type. Therefore it cannot serve as the root of a custom exception hierarchy.

  • class LibraryError(BaseException): pass class FileError(LibraryError): pass class ParseError(LibraryError): pass

    Why it's wrong here

    Subclassing BaseException directly makes every custom library error a sibling of KeyboardInterrupt, SystemExit, and GeneratorExit. Because except Exception does not match BaseException, users cannot catch your errors with the normal exception filter, and they will be caught only by a bare except: block, which typically handles interpreter shutdown and should not be extended. Exception is the documented base class for all user-defined exceptions that are meant to be caught.

  • class LibraryError(Exception): pass class FileError(LibraryError): pass class ParseError(LibraryError): pass

    Why this is correct

    This is the canonical pattern: the library base class derives from Exception, so all library errors are ordinary, catchable exceptions; the specific subclasses then derive from that base class. Code catching LibraryError will also catch FileError and ParseError, while code can still catch either refined type independently. This gives a consistent API and lets the library evolve by adding new specific errors without breaking callers that rely on the base type.

  • class LibraryError(Exception): pass class FileError(Exception): pass class ParseError(Exception): pass

    Why it's wrong here

    Here FileError and ParseError both inherit directly from Exception, bypassing LibraryError entirely. As a result, `except LibraryError` will not capture these errors, so the library base class offers no grouping ability. Also, future refactoring that moves shared functionality into LibraryError will not affect these classes, and callers are forced to list each concrete exception separately.

About these practice questions

Courseiva writes every PCAP question from scratch — 169 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or 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 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.