我是 Python 的新手,我被这个问题困住了。我正在尝试比较两个“异常对象”,例如:
try:
0/0
except Exception as e:
print e
>> integer division or modulo by zero
try:
0/0
except Exception as e2:
print e2
>> integer division or modulo by zero
e == e2
>> False
e is e2
>> False
我应该如何进行这种比较以获得“真”?
我正在尝试做的事情:
class foo():
def bar(self, oldError = None):
try:
return urllib2.urlopen(someString).read()
except urllib2.HTTPError as e:
if e.code == 400:
if e != oldError: print 'Error one'
else:
if e != oldError: print "Error two"
raise
except urllib2.URLError as e:
if e != oldError: print 'Error three'
raise
class someclass():
# at some point this is called as a thread
def ThreadLoop(self, stopThreadEvent):
oldError = None
while not stopThreadEvent.isSet():
try:
a = foo().bar(oldError = oldError)
except Exception as e:
oldError = e
stopThreadEvent.wait(3.0)
(可能是一些语法错误)
我为什么要这样做?因为我不想两次打印相同的错误