如何捕获所有未捕获的异常并继续?

编程入门 行业动态 更新时间:2024-10-23 22:29:44
本文介绍了如何捕获所有未捕获的异常并继续?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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 )

更多推荐

如何捕获所有未捕获的异常并继续?

本文发布于:2023-11-15 14:02:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1595089.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!