Bird
0
0

What will happen when this code runs?

medium📝 Predict Output Q5 of 15
Python - Custom Exceptions
What will happen when this code runs?
class CustomError(Exception):
    def __init__(self, message):
        self.message = message

try:
    raise CustomError('Error occurred')
except CustomError as e:
    print(e.message)
APrints 'CustomError'
BPrints 'Error occurred'
CRaises AttributeError
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Understand custom __init__ storing message

    The CustomError stores the message in self.message during initialization.
  2. Step 2: Raise and catch the exception, then print message

    The except block prints e.message, which is 'Error occurred'.
  3. Final Answer:

    Prints 'Error occurred' -> Option B
  4. Quick Check:

    Custom attribute message prints correctly [OK]
Quick Trick: Store message in __init__ and access via instance [OK]
Common Mistakes:
  • Expecting default exception message printing
  • Forgetting to store message in __init__
  • Trying to print exception object directly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes