Bird
0
0

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:
  1. Step 1: Check Python syntax for custom exceptions

    Custom exceptions must be classes inheriting from Exception or its subclasses.
  2. 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.
  3. Final Answer:

    class MyError(Exception): pass -> Option A
  4. 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'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes