Page Contents
In this tutorial we will learn about Raise keyword in Python .
What is raise keyword in Python ?
The raise
keyword in Python is used to raise an custom Exception . We can also specify what kind of error and text raise
keyword will show .
Syntax of raise keyword in Python
raise Exception(text)
Note :
- If we don’t know what type of error we should raise then we simply use
Exception
. - If we know what type of error we should raise then we will use that specific error such as
TypeError
,IndexError
etc .
Why to use raise keyword in Python ?
Suppose we have written a program which will accept only positive number . But if user gives negative number then the output of the program may go wrong . So after running this program we will get wrong output which is not a matter for a small program but for a big program it is completely waste of time and resources .
So to save the time and resources we can raise an error whenever an user provides a negative number .
# taking user input
number=int(input("Enter a positive number"))
if number<0:
# raising exception if number is neagative
raise Exception("Number can't be negative")
print(number)
First run :
Enter a positive number6 6
Second run:
Enter a positive number-4
--------------------------------------------------------------------------- Exception Traceback (most recent call last) C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_13352/1764935657.py in <module> 3 if number<0: 4 # raising exception if number is neagative ----> 5 raise Exception("Number can't be negative") 6 print(number) Exception: Number can't be negative
- In the above code first we have taken an integer input from the user and stored it in
number
variable . - Then we have checked if the
number
is negative or not . If thenumber
is negative then program will raise an Exception otherwise it will print thenumber
. - After that we have executed the program twice .
- In the first run as we give positive integer as input so the program just printed the positive integer .
- But in the second run as we give negative integer as input so the program has raised an exception with type as
Exception
and text asNumber can't be negative
Examples of raise keyword in Python
Example 1: Use general exception when we don’t know the type of error
To raise general exception in Python we will use Exception
keyword .
raise Exception("Myerror")
--------------------------------------------------------------------------- Exception Traceback (most recent call last) C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_13352/1353725599.py in <module> ----> 1 raise Exception("Myerror") Exception: Myerror
Example 2: Use specified exception when we know the type of error
Raise TypeError in Python
If program need to use a string but we use an integer instead of that string then TypeError
should be raised . In case of of any mismatch in type TypeError
will be raised .
name=1
if type(name)!=str:
raise TypeError("Only strings are allowed")
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_13352/1156069888.py in <module> 1 name=1 2 if type(name)!=str: ----> 3 raise TypeError("Only strings are allowed") TypeError: Only strings are allowed
Raise ZeoDivisionError in Python
When we divide a number with another number then if the another number is 0 then ZeroDivisionError
occurs .
a=10
b=0
if b==0:
raise ZeroDivisionError("Value of divisor can't be 0")
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_13352/1594730086.py in <module> 2 b=0 3 if b==0: ----> 4 raise ZeroDivisionError("Value of divisor can't be 0") ZeroDivisionError: Value of divisor can't be 0
Example 3: Use raise keyword in Python without any error
We can also use raise
keyword in Python without specifying any error .
name="Rajkumar"
number=4
try:
name+number
except:
raise
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_13352/2094443883.py in <module> 3 4 try: ----> 5 name+number 6 except: 7 raise TypeError: can only concatenate str (not "int") to str
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .