Bird
Raised Fist0

Which of the following is the correct syntax to raise a custom exception named MyError?

easy📝 Syntax Q12 of Q15
Python - Custom Exceptions
Which of the following is the correct syntax to raise a custom exception named MyError?
Araise MyError()
Bthrow MyError()
Craise new MyError()
Dthrow new MyError()
Step-by-Step Solution
Solution:
  1. Step 1: Recall the Python keyword to raise exceptions

    Python uses the keyword 'raise' to trigger exceptions, not 'throw'.
  2. Step 2: Check the syntax for raising a custom exception

    Correct syntax is 'raise MyError()' to create and raise the exception instance.
  3. Final Answer:

    raise MyError() -> Option A
  4. Quick Check:

    Raise custom error = raise MyError() [OK]
Quick Trick: Use 'raise' keyword followed by exception instance [OK]
Common Mistakes:
MISTAKES
  • Using 'throw' instead of 'raise'
  • Adding 'new' keyword like in other languages
  • Not calling the exception as a function

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes