Bird
0
0

Find the error in this custom exception usage:

medium📝 Debug Q14 of 15
Python - Custom Exceptions
Find the error in this custom exception usage:
class MyError(Exception):
    pass

try:
    raise MyError("Oops")
except Exception as e:
    print("Error:", e.message)
ACustom exception must not inherit Exception
Bexcept block should catch MyError, not Exception
Craise keyword is missing
DUsing e.message to get error text causes AttributeError
Step-by-Step Solution
Solution:
  1. Step 1: Check exception message access

    In Python, exception objects do not have a message attribute by default.
  2. Step 2: Identify correct way to get message

    The message is accessed by converting the exception to string or using args. Using e.message causes an AttributeError.
  3. Final Answer:

    Using e.message to get error text causes AttributeError -> Option D
  4. Quick Check:

    Exception message accessed via str(e), not e.message [OK]
Quick Trick: Use str(e) to get exception message, not e.message [OK]
Common Mistakes:
  • Assuming e.message exists
  • Thinking custom exceptions can't inherit Exception
  • Missing raise keyword
  • Believing except must catch only MyError

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes