python - Inserting while raising custom exceptions -
if not(my_value < max_limit): print "value of %g , hence invalid. can upto $g" % (my_value, max_limit) raise limitfailurecheck("failed due incorrect value") i have defined custom exception limitfailurecheck in other module. want raise when my_value > max_limt. hence have coded above method. works correctly. want ask give user more info have written print statement states problem is. can same thing while raising custom exception? tried
raise limitfailurecheck("failed due incorrect %g value" % my_value) but raised same statement when printed output.
raise limitfailurecheck("failed due incorrect %g value" % my_value)
i hoping
raise limitfailurecheck("failed due incorrect 99 value") output received:
traceback (most recent call last): file "runtest.py", line 69, in attempt func() file "c:\users\pran\projects\check.py", line 66, in runmytest raise limitfailurecheck('"failed due incorrect %g value" % my_value ) limitfailurecheck
when run python on command line , python exits due unhandled exception, python show traceback , exception string.
when run test program
class limitfailurecheck(exception): pass raise limitfailurecheck('i %s' % 'sad') python says
traceback (most recent call last): file "sad.py", line 4, in <module> raise limitfailurecheck('i %s' % 'sad') __main__.limitfailurecheck: sad notice python displayed line failed , failure message. if don't want behavior, can catch exception , print own message. here's script
class limitfailurecheck(exception): pass try: raise limitfailurecheck('i %s' % 'sad') except limitfailurecheck, e: print 'raise limitfailurecheck("%s")' % e that displays
raise limitfailurecheck("i sad")
Comments
Post a Comment