Python: raise Keyword

The raise keyword is used to raise an exception and stops the execution of the programs. It is very useful when you want to work with the input validations.

Syntax:

if test_condition:
    raise Exception(Statement)

Example:

n = (int(input("Enter a number:")))
if (n%2==0):
    print("The given number is even")
else:
    raise Exception("The number is odd")
    

Output:

Enter a number:3
Exception: The number is odd
Tags