Courseiva
Exceptions and File I/O →easyMultiple Select

PCAP Exceptions and File I/O Practice Question

Which TWO of the following are correct ways to raise a custom exception in Python? (Assume CustomError and CustomException are user-defined.)

⚠ Common exam trap

Python Institute often tests the distinction between raising an exception class versus an exception instance, and the fact that only instances (or classes that are subclasses of `BaseException`) are valid arguments to `raise` — candidates mistakenly think any object can be raised or that raising a class without instantiation is the only correct way.

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

✓

raise CustomError("invalid")

It raises a user-defined exception by instantiating the custom exception class `CustomError` with an argument (the string "invalid"). In Python, the `raise` statement must be followed by an exception instance or an exception class; `CustomError("invalid")` creates an instance of the custom exception, which is the proper way to raise a custom exception with a custom message.

Answer analysis

Option-by-option breakdown

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

  • ✗

    raise "Custom error"

    Why it's wrong here

    In Python 3, the argument to raise must be an exception class or an exception instance; a string literal is neither, so the interpreter immediately raises a TypeError stating that exceptions must derive from BaseException. Even though Python 2 tolerated string exceptions, they were deprecated and removed in Python 3, so this line can never create a custom error.

  • ✗

    raise 42

    Why it's wrong here

    An integer is not a subclass of BaseException and is not an instance of one, so Python rejects it with the same TypeError family. Additionally, a bare number carries no message, traceback context, or identity that would let an except clause recognize it, making it useless for structured error handling.

  • ✓

    raise CustomError("invalid")

    Why this is correct

    This constructs a new instance of the custom exception class immediately before raising it, passing the string "invalid" into the constructor. That value is stored in the instance's args attribute and can be retrieved in an except block, making it the standard, readable way to raise a user-defined exception with diagnostic detail.

  • ✗

    raise Exception

    Why it's wrong here

    Although this is syntactically valid (because Exception is a BaseException subclass), it raises a generic built-in exception rather than any custom type. It is equivalent to calling Exception() with no arguments, so except handlers cannot distinguish this error from unrelated runtime errors, which defeats the purpose of defining a custom exception class.

  • ✓

    raise CustomException()

    Why this is correct

    This is also valid because CustomException() produces an instance of a class that (presumably) subclasses Exception. Unlike the arg-raising version, the instance is created with no constructor arguments, so e.args will be empty; you would typically use this when no additional error detail is needed beyond the exception's class name.

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 →

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.