如何捕获所有未捕获的异常并继续? [英] how to catch all uncaught exceptions and go on?

查看:195
本文介绍了如何捕获所有未捕获的异常并继续?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


编辑:在阅读评论和答案后,我意识到我想做的并不太有意义。我想到的是,我的
在我的代码中有一些地方可能会失败(通常一些请求
调用可能不会去)和我想抓住他们,而不是
把一个尝试:到处都是。我的具体问题是这样的,我将
不在意,如果他们失败,不会影响其余的代码
(比如看门狗电话)。

EDIT: after reading the comments and the answers I realize that what I want to do does not make much sense. What I had in mind was that I have some places in my code which may fail (usually some requests calls which may not go though) and I wanted to catch them instead of putting a try: everywhere. My specific problem was such that I would not care if they fail and would not influence the rest of the code (say, a watchdog call).

我将把这个问题留给后人,以先考虑
真实问题,然后问

I will leave this question for posterity as an ode to "think about the real problem first, then ask"

我正在尝试处理所有未捕获(未处理)的异常:

I am trying to handle all uncaught (otherwise unhandled) exceptions:

import traceback
import sys

def handle_exception(*exc_info):
    print("--------------")
    print(traceback.format_exception(*exc_info))
    print("--------------")

sys.excepthook = handle_exception
raise ValueError("something bad happened, but we got that covered")
print("still there")

这将输出

--------------
['Traceback (most recent call last):\n', '  File "C:/Users/yop/.PyCharm50/config/scratches/scratch_40", line 10, in <module>\n    raise ValueError("something bad happened, but we got that covered")\n', 'ValueError: something bad happened, but we got that covered\n']
--------------

所以,虽然加薪确实被抓住了,但我没有办法想到:调用 handle_exception ,然后继续使用 print(still there)

So, while the raise was indeed caught, it did not work the way I thought: a call to handle_exception, then resume with the print("still there").

我该怎么做?

推荐答案

不要这样做,因为Python调用 sys.excepthook for uncaught 例外

You can't do this because Python calls sys.excepthook for uncaught exceptions.


在控制返回到提示之前;在Python程序中,这在程序退出之前发生。

In an interactive session this happens just before control is returned to the prompt; in a Python program this happens just before the program exits.

没有办法恢复程序的执行或抑制异常在 sys.excepthook

There's no way to resume the execution of the program or "supress" the exception in sys.excepthook.

最近我想到的是

try:
    raise ValueError("something bad happened, but we got that covered")
finally:
    print("still there")

没有子句之外,因此 ValueError 不会被捕获,但是 finally 块保证被执行。因此,异常钩子仍然被调用,'仍然存在'将被打印,但 finally 子句将是执行 sys.excepthook

There's no except clause, therefore ValueError won't be caught, but the finally block is guaranteed to be executed. Thus, the exception hook will still be called and 'still there' will be printed, but the finally clause will be executed before sys.excepthook:


如果异常发生在任何条款中,不处理,
异常被暂时保存。 finally子句被执行。如果
存在一个保存的异常,则在finally
子句的末尾重新生成。

If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The finally clause is executed. If there is a saved exception it is re-raised at the end of the finally clause.

(来自 here

这篇关于如何捕获所有未捕获的异常并继续?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆