Butters Butters - 2 months ago 9
Python Question

User Defined and built in Arguments in exceptions

The following code when executed doesn't result in argument( i.e : Divide by Zero is not permitted ) being printed. It only gives built in error message from-
ZeroDivisionError. So, whats the use of user defined arguments when built in error messages are available.

print "Enter the dividend"
dividend=input()
print "Enter the divisor"
divisor=input()

try:
result=dividend/divisor
except "ZeroDivisonError",argument:
print "Divide by Zero is not permitted \n ",argument # Argument not getting printed
else:
print "Result=%f" %(result)

Answer

The spelling of "ZeroDivisonError" is incorrect and moreover it shouldnt be in "". Correct line:

    except ZeroDivisionError,argument:
    print "Divide by Zero is not permitted \n ",argument
Comments