我有一种情况,我想使用自定义 sys.excepthook。当程序抛出异常时, sys.except 钩子会被调用并做一些事情。
例子:
import sys
def ehook(exctype, value, traceback):
t = 'Keys'
if exctype == AttributeError and value.args[0].split("'")[1] == t:
print "t %s" % (t,)
else:
sys.__excepthook__(exctype, value, traceback)
sys.excepthook = ehook
class Keys():
@staticmethod
def x():
print "this is Keys.x()"
if __name__ == "__main__":
Keys.x()
Keys.noexist()
print "I want to continue here and beyond..."
有没有办法可以取消异常钩子中的活动异常,这样它就不会导致程序退出?