Which of the following is the correct way to raise a custom exception in Python?
Which of the following is the correct way to raise a custom exception in Python?
Solution
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.
Related questions:
2025: 20 May Shift 1