class ErrorCodeException(Exception):
def __init__(self, code, message):
super().__init__(message)
self.code = code inherits Exception and calls super().__init__(message) to set the message properly.
Step 2: Compare with other options
class ErrorCodeException(Exception):
def __init__(self, code, message):
self.code = code
self.message = message does not call super().__init__, so message may not behave like a normal exception message. class ErrorCodeException:
def __init__(self, code, message):
self.code = code
self.message = message lacks inheritance. class ErrorCodeException(Exception):
pass has no code or message storage.
Final Answer:
class ErrorCodeException(Exception):
def __init__(self, code, message):
super().__init__(message)
self.code = code -> Option C
Quick Check:
Inherit Exception and call super() with message = B [OK]
Quick Trick:Call super().__init__(message) to set exception message [OK]
Common Mistakes:
Not calling super().__init__ for message
Not inheriting from Exception
Storing message without Exception support
Master "Custom Exceptions" in Python
9 interactive learning modes - each teaches the same concept differently