Hi guys am just a beginner in python.I studied about exception handling and it found so difficult for me. My code is
def exceptionhandling(z,y):
hd = z
hs = y
try:
sd = hd/hs
except ZeroDivisionError as err
print "this cannot be possible".err
And when i called the function exceptionhandling(1,0)
i didnt get the expected output "this cannot be possible".
PLease help me in understanding this one ..Am new to so so am sorry if this is a low standard one.
You try to call err method of string. Actually, it doesn't has it) Better way to do this is
print "this cannot be possible %s" % err.message
Also I hope that you don't forget about indentations.
There exists some typos in your code:
#first indent your code correctly
def exceptionhandling(z,y):
hd = z
hs = y
try:
sd = hd/hs
return sd #return the result if you get it without exception
except ZeroDivisionError as err: #need ":" at the end of line
print "this cannot be possible", err #print multiple strings with commas
running demo:
In [59]: exceptionhandling(0, 3) #0/3 is ok
In [60]: exceptionhandling(4, 0) #4/0 is not allowed
this cannot be possible integer division or modulo by zero
You will get err
as exception object, you can get the detail value of that using err.args
.
args
is tuple of 2 element, first is code and second is description.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.