Which of the following is the correct way to define a custom exception in Python?
easy📝 Syntax Q12 of 15
Python - Custom Exceptions
Which of the following is the correct way to define a custom exception in Python?
Aclass MyError(Exception): pass
Bdef MyError(): pass
Cclass MyError: pass
Dexception MyError(Exception): pass
Step-by-Step Solution
Solution:
Step 1: Check Python syntax for custom exceptions
Custom exceptions must be classes inheriting from Exception or its subclasses.
Step 2: Evaluate each option
A defines a class without inheritance, B defines a function, D uses invalid keyword. Only C ('class MyError(Exception): pass') correctly defines a custom exception.
Final Answer:
class MyError(Exception): pass -> Option A
Quick Check:
Custom exceptions are classes inheriting Exception = C [OK]
Quick Trick:Custom exceptions are classes inheriting Exception [OK]
Common Mistakes:
Defining exceptions as functions
Not inheriting from Exception
Using invalid keywords like 'exception'
Master "Custom Exceptions" in Python
9 interactive learning modes - each teaches the same concept differently