Exception handling in python
*************************
or error
to avoid the abnormal termination of the program
* syntax error
while true:
True ==>
* logical error
/
//
* run time error
*************
ex:1
a=int(input("enter a"))
b=int(input("enter b"))
c=a/b
print(c)
ex:2
c=0
a=int(input("enter a"))
b=int(input("enter b"))
try:
c=a/b
except:
print('error occured, may be zero divide')
print(c)
ex:3
a=int(input("enter a"))
b=int(input("enter b"))
try:
c=a/b
print(c)
except ZeroDivisionError:
print("zero division error")
ex:4 ( multi except)
try:
a=int(input("enter a"))
b=int(input("enter b"))
c=a/b
print(c)
except ZeroDivisionError:
print("zero division error")
except ValueError:
print("check the input")
ex:5 (finally)
try:
a=int(input("enter a"))
b=int(input("enter b"))
c=a/b
print(c)
except ZeroDivisionError:
print("zero division error")
except ValueError:
print("check the input")
finally:
print(" thank you")
Raising Exception
****************
The raise statement allows the programmer to force a specific exception to occur.
ex:
try:
raise NameError("Hi there") # Raise Error
except NameError:
print ("An exception")