Bird
Raised Fist0

How can you create a hierarchy of custom exceptions where DatabaseError inherits from ApplicationError, which inherits from Exception?

hard🚀 Application Q9 of Q15
Python - Custom Exceptions
How can you create a hierarchy of custom exceptions where DatabaseError inherits from ApplicationError, which inherits from Exception?
Aclass ApplicationError():\n pass\nclass DatabaseError(ApplicationError):\n pass
Bclass DatabaseError(Exception):\n pass\nclass ApplicationError(DatabaseError):\n pass
Cclass ApplicationError(Exception):\n pass\nclass DatabaseError(ApplicationError):\n pass
Dclass ApplicationError(Exception):\n pass\nclass DatabaseError(Exception):\n pass
Step-by-Step Solution
Solution:
  1. Step 1: Understand inheritance order for exception hierarchy

    ApplicationError should inherit from Exception, DatabaseError from ApplicationError.
  2. Step 2: Check options for correct inheritance chain

    class ApplicationError(Exception):\n pass\nclass DatabaseError(ApplicationError):\n pass correctly defines ApplicationError inheriting Exception, then DatabaseError inheriting ApplicationError.
  3. Final Answer:

    class ApplicationError(Exception):\n pass\nclass DatabaseError(ApplicationError):\n pass -> Option C
  4. Quick Check:

    Subclass inherits from parent exception class [OK]
Quick Trick: Subclass inherits from parent exception class [OK]
Common Mistakes:
MISTAKES
  • Reversing inheritance order
  • Not inheriting from Exception at top level
  • Omitting inheritance parentheses

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes