Bird
Raised Fist0

You want to create a custom exception MyError and raise it with a message. Which code correctly does this?

hard🚀 Application Q8 of Q15
Python - Advanced Exception Handling
You want to create a custom exception MyError and raise it with a message. Which code correctly does this?
Aclass MyError: pass raise MyError('Custom error occurred')
Bdef MyError(): pass raise MyError('Custom error occurred')
Cclass MyError(Exception): pass raise MyError('Custom error occurred')
Draise Exception('MyError: Custom error occurred')
Step-by-Step Solution
Solution:
  1. Step 1: Define custom exception by inheriting Exception

    Custom exceptions must inherit from Exception class.
  2. Step 2: Raise the custom exception with a message

    Use raise MyError('message') after defining the class.
  3. Final Answer:

    class MyError(Exception): pass\nraise MyError('Custom error occurred') -> Option C
  4. Quick Check:

    Custom exception inherits Exception = C [OK]
Quick Trick: Custom exceptions inherit Exception class [OK]
Common Mistakes:
MISTAKES
  • Defining exception as function or without inheritance
  • Raising generic Exception instead of custom
  • Not passing message when raising

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes