Bird
Raised Fist0

Which of the following is the correct way to define a custom exception named MyError in Python?

easy📝 Syntax Q12 of Q15
Python - Custom Exceptions
Which of the following is the correct way to define a custom exception named MyError in Python?
Adef MyError(): pass
Bclass MyError(Exception): pass
Cclass MyError: pass
Dexception MyError(Exception): pass
Step-by-Step Solution
Solution:
  1. Step 1: Recall syntax for custom exceptions

    Custom exceptions are classes that inherit from Exception or its subclasses.
  2. Step 2: Identify correct class definition

    class MyError(Exception): pass correctly defines MyError as a subclass of Exception with pass to keep it simple.
  3. Final Answer:

    class MyError(Exception): pass -> Option B
  4. Quick Check:

    Custom exception = class inheriting Exception [OK]
Quick Trick: Custom exceptions are classes inheriting Exception [OK]
Common Mistakes:
MISTAKES
  • Defining exception as a function
  • Not inheriting from Exception
  • Using wrong keyword like 'exception'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes