Comparing Exception Objects in Python -
i new @ python , i'm stuck @ problem. trying compare 2 "exception objects", example:
try: 0/0 except exception e: print e >> integer division or modulo 0 try: 0/0 except exception e2: print e2 >> integer division or modulo 0 e == e2 >> false e e2 >> false
how should perform comparison obtain "true"?
what trying do:
class foo(): def bar(self, olderror = none): try: return urllib2.urlopen(somestring).read() except urllib2.httperror e: if e.code == 400: if e != olderror: print 'error one' else: if e != olderror: print "error two" raise except urllib2.urlerror e: if e != olderror: print 'error three' raise class someclass(): # @ point called thread def threadloop(self, stopthreadevent): olderror = none while not stopthreadevent.isset(): try: = foo().bar(olderror = olderror) except exception e: olderror = e stopthreadevent.wait(3.0)
(probably syntax error)
why doing that? because don't want print same error twice
with exception classes, can test functional equality with
type(e) type(e2) , e.args == e2.args
this tests classes identical, , contain same exception arguments. may not work exception classes don't use args
, knowledge, standard exceptions do.
Comments
Post a Comment