raise keyword in python by R4R Team

-An exception can be raised by using the raise keyword in python.

Syntax-

raise Exception_class,


program-

try:
n = int(input("Enter the numbern"))
if n<0:
raise ValueError;
else:
print("Number is positive")
except ValueError:
print("Number is negative")


output-

Enter the number
4
Number is positive

Enter the number
-3
Number is negative


-In this program, we check number is positive or not, and we raise a exception if number is negative.
-By doing this or by raise in python, we defined user level exception.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!