Handle Exception In Python
# program for exception handling
number_1 = int(input("Enter number 1 : "))
number_2 = int(input("Enter number 2 : "))
result = number_1 / number_2
print("result : ", result)
#output
Enter number 1 : 10
Enter number 2 : 0
Traceback (most recent call last):
File "C:/Self_Practice_Er_M_S_Dandyan/Python/Pycharm/xml/main.py", line 6, in <module>
result = number_1 / number_2
ZeroDivisionError: division by zero
- Try and except block
It is a combination of try and except, exception thrown in statement under try, will handover to except blog
Syntax :
Case 1
try :
----------statement1
except Exception class_name :
---------- statement related to handle part
#program for exception handling
try :
number_1 = int(input("Enter number 1 : "))
number_2 = int(input("Enter number 2 : "))
result = number_1 / number_2
print("result : ", result)
except ZeroDivisionError: # this named except block, it will handle DIVIDE by Zero exception
print("number_2 can not be zero")
#Output
Enter number 1 : 10
Enter number 2 : 0
number_2 can not be zero
Case 2
try :
----------statement1
----------statement 2
except Exception class_name :
---------- statement related to handle part
except :
---------- statement related to handle part
#program for exception handling
try :
number_1 = int(input("Enter number 1 : "))
number_2 = int(input("Enter number 2 : "))
result = number_1 / number_2
print("result : ", result)
except ZeroDivisionError: # this named default block, it will handle DIVIDE by Zero exception
print("number_2 can not be zero")
except : # This is default except block, it will handle all type of exception
print("Please enter a valid integer number")
Handling Exception with specific class
#Output
Enter number 1 : 10
Enter number 2 : 0
number_2 can not be zero
Handling Exception with Default (handel all type of exception)
#Output
Enter number 1 : 10
Enter number 2 : 10.25
Please enter a valid integer number
- Try with else block
It is a combination of try and except with else blog, exception thrown in statement under try, will handover to except blog and then else blog will be executed only in case of no exception thrown.
Syntax :
try :
----------statement1
----------statement 2
except Exception class_name :
---------- statement related to handle part
else :
---------- statement related to else part.
#program for try and else block, else part will be getting executed when no exception thrown
try :
number_1 = int(input("Enter number 1 : "))
number_2 = int(input("Enter number 2 : "))
result = number_1 / number_2
print("result : ", result)
except ZeroDivisionError: # this named default block, it will handle DIVIDE by Zero exception
print("number_2 can not be zero")
except : # This is default except block, it will handle all type of exception
print("Please enter a valid integer number")
else :
print("In else block, No Exception thrown in try block ")
when exception encounter
#Output
Enter number 1 : 10
Enter number 2 : 0
number_2 can not be zero
when No-Exception encountered
#Output
Enter number 1 : 10
Enter number 2 : 5
result : 2.0
In else block, No Exception thrown in try block
- Try with finally block
It is a combination of try and except with finally blog , exception thrown in statement under try, will handover to except blog.
finally blog will execute in both conditions, exception thrown or not
Syntax :
try :
----------statement1
----------statement 2
except Exception class_name :
---------- statement related to handle part
finally :
---------- statement related to final part.
# program for exception handling with try and finally block
try :
number_1 = int(input("Enter number 1 : "))
number_2 = int(input("Enter number 2 : "))
result = number_1 / number_2
print("result : ", result)
except ZeroDivisionError: # this named default block, it will handle DIVIDE by Zero exception
print("number_2 can not be zero")
except : # This is default except block, it will handle all type of exception
print("Please enter a valid integer number")
finally:
print("In finally block, this block will execute and independent of exception occurance or non-occurance")
When exception occurs
#Output
Enter number 1 : 10
Enter number 2 : 0
number_2 can not be zero
In finally block, this block will execute and independent of exception occurance or non-occurance
When no exception occurs
#Output
Enter number 1 : 10
Enter number 2 : 5
result : 2.0
In finally block, this block will execute and independent of exception occurance or non-occurance
Comments
Post a Comment