Skip to main contentSkip to solution

Which of the following is the correct way to raise a custom exception in Python?

Solution

✅ Correct Option: 1

Python uses the raise keyword to throw an exception. You write raise followed by the exception class and an optional message, for example raise CustomError("Custom Error Message"), where Exception-name stands for the class name. A custom exception is a class that inherits from Exception.


Option 2: throw new is Java, C++ and JavaScript syntax. Python has no throw keyword and no new keyword.

Option 3: except handles an exception that has already been raised. It does not raise one.

Option 4: catch is used in Java, C++ and JavaScript. Python's equivalent is except, and neither raises an exception.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question