Bird
0
0

Which of these is a correct way to raise a custom exception named MyError?

easy📝 Conceptual Q2 of 15
Python - Custom Exceptions
Which of these is a correct way to raise a custom exception named MyError?
Araise MyError('An error occurred')
Bthrow MyError('An error occurred')
Craise Exception('MyError')
Dthrow Exception('MyError')
Step-by-Step Solution
Solution:
  1. Step 1: Recall Python syntax for raising exceptions

    In Python, exceptions are raised using the raise keyword followed by the exception instance.
  2. Step 2: Identify the correct syntax for custom exceptions

    raise MyError('An error occurred') correctly uses raise MyError('message'). Options A and B use throw, which is not Python syntax. raise Exception('MyError') raises a generic Exception, not the custom one.
  3. Final Answer:

    raise MyError('An error occurred') -> Option A
  4. Quick Check:

    Raise custom exception = raise MyError(...) [OK]
Quick Trick: Use 'raise' keyword to throw exceptions in Python [OK]
Common Mistakes:
  • Using 'throw' instead of 'raise'
  • Raising generic Exception instead of custom
  • Missing parentheses when raising

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes