Python 扭曲';s trap方法引发的异常不';不要被抓住
在我看来,failure.trap方法引发了无法用try/except块捕获的异常Python 扭曲';s trap方法引发的异常不';不要被抓住,python,twisted,Python,Twisted,在我看来,failure.trap方法引发了无法用try/except块捕获的异常 from twisted.internet import defer def someAsynchronousMethod(_): print 'I am an asynchronous method and I raise a ZeroDivisionError!!!' raise ZeroDivisionError def __errorHandler(failure): pri
from twisted.internet import defer
def someAsynchronousMethod(_):
print 'I am an asynchronous method and I raise a ZeroDivisionError!!!'
raise ZeroDivisionError
def __errorHandler(failure):
print 'I am __errorHandler number 1 !!!!'
try:
# Traps only NameError, other Errors will be immediately reraised
failure.trap(NameError)
# This code executes only if Exception has been trapped
ret = {'errno': 666,
'errmsg': 'NameError'
}
# It looks like this code will be executed if Exception has not been trapped
# instead it was reraised and should be caught here, but it turns out it
# doesn't get caught, instead the method returns None at this point
except Exception:
# This code is never reached
ret = {'errno': 5000,
'errmsg': 'OtherError'
}
# This code is reached if Exception has been trapped
return ret
def __successHandler(result):
print 'Successful execution !!!!'
return result
def __successHandler2(result):
print """I expect to receive ret = {'errno': 5000, 'errmsg': 'OtherError'},
because __errorHandler number 1 is supposed to return a dict !!!!"""
return result
def __errorHandler2(failure):
print 'I am __errorHandler number 2 !!!!'
return failure
d = defer.Deferred()
# Method that raises an Error
d.addCallback(someAsynchronousMethod)
d.addCallbacks(__successHandler, __errorHandler)
d.addCallbacks(__successHandler2, __errorHandler2)
d.callback(0)
输出:
我是一个异步方法,我提出了一个错误!!!
我是一号错误处理者!!!!
我是2号错误处理者!!!!
延迟中未处理的错误:
未处理错误
回溯(最近一次呼叫最后一次):
文件“twistedFailures.py”,第58行,在
d、 回调(0)
回调中的文件“/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py”,第382行
自启动返回(结果)
文件“/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py”,第490行,在startRunCallbacks中
self.\u runCallbacks()
--- ---
文件“/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py”,第577行,在运行回调中
current.result=回调(current.result,*args,**kw)
文件“twistedFailures.py”,第8行,在someAsynchronousMethod中
调零误差
异常。零错误:
我期望failure.trap方法引发的任何异常都会被捕获,但我错了
Twisted在幕后有什么具体的方法来处理这个问题吗?如果是,原因是什么?您预期
失败。陷阱重新引发零分区错误
异常。但这只发生在Python3中:在Python2中,failure.trap
实际上重新引发了failure
。政府对此非常清楚:
在Python 2中,将引发故障;在Python3中,将重新引发底层异常
Twisted的类不从继承,因此您的异常除外:
不会捕获它。您需要而不是(异常、失败):
I am an asynchronous method and I raise a ZeroDivisionError!!!
I am __errorHandler number 1 !!!!
I am __errorHandler number 2 !!!!
Unhandled error in Deferred:
Unhandled Error
Traceback (most recent call last):
File "twistedFailures.py", line 58, in <module>
d.callback(0)
File "/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py", line 382, in callback
self._startRunCallbacks(result)
File "/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py", line 490, in _startRunCallbacks
self._runCallbacks()
--- <exception caught here> ---
File "/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py", line 577, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "twistedFailures.py", line 8, in someAsynchronousMethod
raise ZeroDivisionError
exceptions.ZeroDivisionError: