如何使python脚本在sys.excepthook之后继续工作?
我想定制错误处理程序,之后脚本将继续工作。我尝试使用sys.excepthook=CatchHook,得到了如下结果:如何使python脚本在sys.excepthook之后继续工作?,python,exception,hook,traceback,Python,Exception,Hook,Traceback,我想定制错误处理程序,之后脚本将继续工作。我尝试使用sys.excepthook=CatchHook,得到了如下结果: def your_script(): while True: try: your_code() except Exception as ex: template = "An exception of type {0} occurred. Arguments:\n{1!r}"
def your_script():
while True:
try:
your_code()
except Exception as ex:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(ex).__name__, ex.args)
print(message)
def捕捉钩(e、v、t):
打印(“{}/{}/{}.”格式(str(e.{{u name},str(v),str(traceback.extract_tb(t)))
打印(“已处理回溯,但脚本将继续工作!”)
sys.excepthook=CatchHook
但当我运行它时,它会退出。我如何才能让它在不退出的情况下工作?我如何理解您想要这样的东西:
def your_script():
while True:
try:
your_code()
except Exception as ex:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(ex).__name__, ex.args)
print(message)
如果要退出“while”,请使用break或return您是否尝试过
尝试除了block@Joe我需要一个全局钩子来捕获所有内容,包括用户对eval()的输入不,我需要sys.excepthook或它的替代品,因为我有动态代码,需要捕获每一行的所有内容,而不是try
和catch
。