Bird
Raised Fist0

Why is it important that custom exceptions inherit from the built-in Exception class in Python?

hard🧠 Conceptual Q10 of Q15
Python - Custom Exceptions
Why is it important that custom exceptions inherit from the built-in Exception class in Python?
ABecause only classes inheriting Exception can be caught by except blocks
BBecause it makes the program run faster
CBecause Python requires all classes to inherit Exception
DBecause it automatically logs errors to a file
Step-by-Step Solution
Solution:
  1. Step 1: Understand exception hierarchy

    Python's error handling catches exceptions derived from Exception class.
  2. Step 2: Importance of inheritance

    Custom exceptions must inherit Exception to be recognized and caught by except blocks.
  3. Final Answer:

    Because only classes inheriting Exception can be caught by except blocks -> Option A
  4. Quick Check:

    Inheritance from Exception enables catching custom exceptions [OK]
Quick Trick: Inherit Exception so your custom errors can be caught [OK]
Common Mistakes:
MISTAKES
  • Thinking inheritance affects speed
  • Believing all classes must inherit Exception
  • Assuming automatic error logging

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes