Python 调用自定义sys.except_hook后是否可以阻止退出?
问题代码:Python 调用自定义sys.except_hook后是否可以阻止退出?,python,Python,问题代码: import sys def handler(exception_type, exception_value, traceback_object): print(exception_type, exception_value) sys.excepthook = handler defined_thing = "Redacted faster than dissenting tweets" print(defined_thing) print(undefined_th
import sys
def handler(exception_type, exception_value, traceback_object):
print(exception_type, exception_value)
sys.excepthook = handler
defined_thing = "Redacted faster than dissenting tweets"
print(defined_thing)
print(undefined_thing)
print("I wish I got here...")
结果:
Redacted faster than dissenting tweets
<class 'NameError'> name 'undefined_thing' is not defined
Process finished with exit code 1
编辑速度比反对推文快
未定义名称“undefined_thing”
进程已完成,退出代码为1
问题:我是否可以阻止退出并继续打印错误?有关该功能,请参阅:
sys.excepthook(类型、值、回溯)
此函数将给定的回溯和异常打印到sys.stderr
它只提供额外的调试功能。否。
来自(我的):
当引发异常并且取消捕获时,解释器调用
sys.excepthook
有三个参数,exception类,exception
实例和一个回溯对象。在本周的互动会议中
在控件返回到提示符之前发生您的Exception钩子将运行,然后程序将退出。如果你想捕捉异常,你需要使用通常的公式。把政治拖到这里有好的理由吗?绝对没有。