How to Raise Exceptions in Python?


The raise statement enables a programmer to compel the occurrence of a specified exception. Raise’s single argument specifies the exception to be raised. This must be an exception instance or a class of exceptions (a class that derives from Exception).

Example:

try:

    raise NameError("Hi t") 

except NameError:

    print ("An_exception")

    raise  

Output:

An_exception


Leave a Reply

Your email address will not be published. Required fields are marked *