重新提炼Python异常并保留堆栈跟踪

编程入门 行业动态 更新时间:2024-10-06 20:35:59
本文介绍了重新提炼Python异常并保留堆栈跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图捕获一个线程中的一个异常,并在主线程中重新提出:

import threading import sys class FailingThread(threading.Thread): def run(self): try: raise ValueError('x')除了ValueError: self.exc_info = sys.exc_info() failingThread = FailingThread() failingThread.start() failingThread.join() print failingThread.exc_info raise failingThread.exc_info [1]

这基本上可以产生以下输出:

(< type'exceptions.ValueError '> ValueError('x',),< traceback对象在0x1004cc320>)追溯(最近的最后一次调用):文件test.py,第16行,<模块> ; raise failingThread.exc_info [1]

但是,异常的来源指向行16,重新发生的地方。原来的例外来自第7行。如何修改主要线程,以便输出如下:

追溯(最近的最后一次调用):文件test.py,第7行,< module>

解决方案

您需要使用所有三个参数来加注: / p>

raise failingThread.exc_info [0],failingThread.exc_info [1],failingThread.exc_info [2]

传递跟踪对象作为第三个参数保留堆栈。

从 help('raise'):

如果第三个对象存在而不是无,它必须是追溯对象(请参阅标准类型层次结构),它是代替当前位置作为异常发生的地方。如果第三个对象存在,而不是 traceback对象或 None ,则会引发 TypeError 异常。 三个表达式的 raise 可以在except子句中透明地重新引发一个异常,但是 raise 没有表达式应该是首选,如果重新提出的异常是当前范围内最多最近活动的异常。

在这种特殊情况下,您不能使用no表达式版本。

I'm trying to catch an exception in a thread and re-raise it in the main thread:

import threading import sys class FailingThread(threading.Thread): def run(self): try: raise ValueError('x') except ValueError: self.exc_info = sys.exc_info() failingThread = FailingThread() failingThread.start() failingThread.join() print failingThread.exc_info raise failingThread.exc_info[1]

This basically works and yields the following output:

(<type 'exceptions.ValueError'>, ValueError('x',), <traceback object at 0x1004cc320>) Traceback (most recent call last): File "test.py", line 16, in <module> raise failingThread.exc_info[1]

However, the source of the exception points to line 16, where the re-raise occurred. The original exception comes from line 7. How do I have to modify the main thread so that the output reads:

Traceback (most recent call last): File "test.py", line 7, in <module>

解决方案

You need to use all three arguments to raise:

raise failingThread.exc_info[0], failingThread.exc_info[1], failingThread.exc_info[2]

passing the traceback object in as the third argument preserves the stack.

From help('raise'):

If a third object is present and not None, it must be a traceback object (see section The standard type hierarchy), and it is substituted instead of the current location as the place where the exception occurred. If the third object is present and not a traceback object or None, a TypeError exception is raised. The three-expression form of raise is useful to re-raise an exception transparently in an except clause, but raise with no expressions should be preferred if the exception to be re-raised was the most recently active exception in the current scope.

In this particular case you cannot use the no expression version.

更多推荐

重新提炼Python异常并保留堆栈跟踪

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

发布评论

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

>www.elefans.com

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