Courseiva
Question 2 of 169
Exceptions and File I/OhardMultiple ChoiceObjective-mapped

Custom Exception Classes in Python — Proper Inheritance and Message Handling

A developer implements a custom exception class `DataError` that inherits from `Exception`. Which method override is essential to ensure the exception message is properly displayed when caught?

Quick Answer

The correct choice is to override __init__ to accept a message and call super().__init__(message). This is essential because the built-in Exception class stores any passed message in its args attribute, and its default __str__ method relies on that attribute to display the error text when the exception is caught and printed. Without explicitly calling the parent constructor, your custom exception class would not inherit this message-handling behavior, and the message would be lost or not shown. On the Certified Associate Python Programmer PCAP exam, this tests your understanding of proper inheritance in Python’s exception hierarchy, often appearing as a multiple-choice trap where a candidate might override __str__ instead of __init__. The common mistake is forgetting that the base class already handles string representation, so you only need to ensure the message reaches it. Memory tip: “Init it to pass it”—override __init__ to accept the message, then pass it up with super().__init__().

⚠ Common exam trap

Python Institute often tests the misconception that you must override `__str__` to display a custom message, when in fact the base `Exception.__init__` handles message storage and display automatically if called correctly.

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

Override __init__ to accept a message and call super().__init__(message).

The `Exception` class's `__init__` method stores the message argument in the `args` attribute, which is used by the default `__str__` method to display the message. By overriding `__init__` to accept a message and call `super().__init__(message)`, the custom exception properly passes the message to the base class, ensuring it is displayed when caught and printed.

Answer analysis

Option-by-option breakdown

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

  • Override __init__ to accept a message and call super().__init__(message).

    Why this is correct

    This ensures the message is stored and displayed.

  • Set the __cause__ attribute in __init__.

    Why it's wrong here

    __cause__ is for exception chaining, not message display.

  • Override __str__ to return a formatted string.

    Why it's wrong here

    Exception already provides __str__ if message is passed to init.

  • Override __repr__ to return a detailed representation.

    Why it's wrong here

    __repr__ is not used for displaying exception messages.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 more way this is tested on PCAP

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. A developer is implementing a custom exception for invalid data. Which class should the custom exception inherit from?

medium
  • A.RuntimeError
  • B.ArithmeticError
  • C.BaseException
  • D.Exception

Why D: The `Exception` class is the base class for all built-in, non-system-exiting exceptions in Python. Custom exceptions should inherit from `Exception` (or one of its subclasses) to ensure they are caught by generic `except Exception:` handlers and integrate properly with Python's exception hierarchy, while avoiding the system-exiting exceptions derived from `BaseException`.

Last reviewed: Jun 30, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

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.