EXCEPTION HANDLING IN PYTHON
Python Errors And Built-In Exception
Exception can be said
to be any abnormal condition in a program resulting to the disruption in the
flow of the program.
· Base Exception
] Exception
§ Arithmetic Error
Ø Floating point error
Ø Overflow error
Ø Zero division error
§ Assertion error
Syntax:
try:
#write some code
#that might throw exception
except Exception 1:
#exception
handler,alert the user
except Exception 2:
#exception
handler,alert the user
........
........
except Exception N:
#exception
handler,alert the user
except:
#exception
handler,alert the user
else:
in
case of no error,execute the else block code
EXAMPLE:
try:
fbp=open('letter.txt','r')
print(fbp.read())
fbp.close()
except IO Error:
print('letter.text
is not valid file,file not found')
Try ....Finally
try:
<body
of the try block>
except<exception type1>:
<handler>
except<exception typeN>:
<handlerN>
except:
<handlerexcept>
else:
<process_else>
finally:
<process_finally>
Program
try:
N1,N2=eval(input("enter 2
numbers,seperated by comma:"))
res=N1/N2
except ZeroDivisionerror:
print("division
by zero is error!!")
res=0
except Syntaxerror:
print("comma is
missing.Input Value seperated by comma like this 3,4")
#all other exception
except:
print("Wrong
input")
else:
print("no
exception")
finally:
print("N1
is{},N2 is{},Result is{}".format(N1,N2,res))
OUTPUT:
#input1:-
enter two numbers,seperated by comma:3,4
no exception
N1 is 3,N2 is 4,result is 0.75
#input2:-
enter two numbers,seperated by comma:0,3
no exception
N1 is 0,N2 is 3,result is 0.0
#input3:-
enter two numbers,seperated by comma:4,0
division by zero is error1!!
N1 is 4,N2 is 0,result is 0
Creating User-Defined Exception
syntax:
class<Base ErrorClassName>(exception):
"""Base class for other exception"""
Pass
Program
class MyError(Exception):
pass
class ZeroValueError(Exception):
pass
while True:
try:
i_num=int(input("enter a number:"))
if i_num==0:
raise
ZeroValueError
if i_num<0:
raise MyError
else:
print("valid input")
break
except MyError:
print("value should not be negative
number........,try again!")
except ZeroValueError:
print("value should not be
Zero........,try again!")
NOTE:-
In the above example "My error and""zero value
error"are user defined exception.
output:
enter a number :-5
value should be positive
number........,try again!
enter a number :0
value should not be positive
Zero........,try again!
enter a number :3
valid input
Example for user defined exception type II
#Define
Python user defined exception
class Error(Exception):
pass
class ValueTooSmallError(Error):
pass
class ValueTooLargeError(Error):
pass
while True:
try:
i_num=int(input("enter a number:"))
number=10
if i_num<number:
raise ValueTooSmallError
elif i_num>number:
raise ValueTooLargeError
break
except
ValueTooSmallError:
print("this value is too small,try again!")
print()
except
ValueTooLargeError:
print("this value is too large,try again!")
print()
print("Congratulations!You guessed
it correctly.")
output:
Enter a number:12
this value is too large,try again!
Enter a number:0
this value is too small,try again!
Enter a number:8
this value is too small,try again!
Enter a number:10
Congratulations!You guessed it
correctly
0 comments:
Post a Comment