Python raise keyword

python raise keyword

 

Python raise keyword. In python we can raise exception by using raise keyword. We have already gone through detailed overview on in-built Python exceptions and how it is being handled with python try except & finally statements.

 

All these exceptions are default and python interpreter decide when to raise the exception but WHAT if you want to raise an exception?


Python raise

Python gives your flexibility to raise exception whenever you want in the program. It is very powerful keyword and having unique features in it.

 

Mostly people are using python raise – keyword in conditional block to decide when and where to raise an exception.

Syntax:

raise  <exception>

 

When you specify raise keyword in your code and the moment program execution reaches that point – immediately exception is being raised by skipping further statements. 

An interpreter doesn’t see, whether do you really have an error in your code or not. It simply raises an exception as per specified exception name.

 

Example

Python_raise.py

var=10

print(“Var= “, var)

raise ValueError 

print(“Program execution continues….”)

Output:

Var=  10

Traceback (most recent call last):

 File “C:/Users/Documents/Python_raise.py”, line 3, in <module>

 raise ValueError

ValueError

>>> 

 

Python raise keyword most often used in try block for various reasons for instance, to cross verify whether exceptions are properly getting raised and check its execution flow, to avoid the program execution termination, etc.

 

python_raise.py

try:

 var=10

 print(“Var= “, var)

 raise ValueError

except ValueError:

 print(“Oops!! Value error raised “) 

print(“Program execution continues….”)

 

Output

Var=  10

Oops!! Value error raised

Program execution continues….



Use raise keyword in try except

 

It is very effective way to check whether your exceptional handling code – try/except block working properly without involving end user.

If there would be chances of an exception can occur in your code where you already applied, try/except blocks but you want to test it then you can add raise keyword there.

 

Observe this example – We are performing division of two numbers by taking inputs from users.

In this case we know ValueError exception can occur if user enter non-digit number. We have already seen this example here.

Now we could test this exception by raising ValueError exception manually in the code, irrespective of user inputs. 

Python_raise.py

try:

 N1=int(input(“Enter first Number – N1 :”))

 N2=int(input(“Enter Second Number – N2 :”))

 Result=N1/N2

 print(“Result :”, Result)

 raise ValueError

except ValueError:

 print(“Oops!! Seems you have entered invalid Number”)

print(“Program execution continues….”)


Note this result – we have entered correct input values still ValueError exception encountered due to raise keyword specified in try block code.

Output

Enter first Number – N1 :100

Enter Second Number – N2 :12

Result : 8.333333333333334

Oops!! Seems you have entered invalid Number

Program execution continues….

>>> 

Spread the love

1 thought on “Python raise keyword”

Leave a Comment