Bird
0
0

You want to create a custom exception ValidationError that stores an error code along with the message. Which code correctly implements this?

hard📝 Application Q15 of 15
Python - Custom Exceptions
You want to create a custom exception ValidationError that stores an error code along with the message. Which code correctly implements this?
Aclass ValidationError(Exception): def __init__(self, message, code): super().__init__(message) self.code = code
Bclass ValidationError(Exception): def __init__(self, message, code): self.message = message self.code = code
Cclass ValidationError(Exception): def __init__(self, code): super().__init__(code) self.message = ''
Dclass ValidationError(Exception): def __init__(self, message): super().__init__(message) self.code = None
Step-by-Step Solution
Solution:
  1. Step 1: Understand storing extra info in custom exceptions

    We want to keep both message and code, so __init__ must accept both.
  2. Step 2: Properly call super().__init__ with message and store code

    class ValidationError(Exception): def __init__(self, message, code): super().__init__(message) self.code = code calls super().__init__(message) to set message and saves code as attribute.
  3. Final Answer:

    class ValidationError(Exception): def __init__(self, message, code): super().__init__(message) self.code = code -> Option A
  4. Quick Check:

    Call super with message, store extra attributes separately [OK]
Quick Trick: Call super with message, save extra data as attributes [OK]
Common Mistakes:
  • Not calling super().__init__ with message
  • Not storing extra info as attributes
  • Mixing message and code parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes