Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使python脚本在sys.excepthook之后继续工作?_Python_Exception_Hook_Traceback - Fatal编程技术网

如何使python脚本在sys.excepthook之后继续工作?

如何使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}"

我想定制错误处理程序,之后脚本将继续工作。我尝试使用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)
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