从异常对象提取回溯信息 [英] Extract traceback info from an exception object

查看:137
本文介绍了从异常对象提取回溯信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个Exception对象(来源不明)有没有办法获得它的追溯?我有这样的代码:

  def stuff():
try:
.....
返回有用的
除了异常作为e:
返回e

result = stuff()
如果isinstance(结果,异常):
result.traceback< - 如何?

一旦我拥有它,我如何从异常对象中提取追溯?

解决方案

追踪不存储在异常中。 (不是,也就是说,在Python 2中;有关Python 3的情况,请参阅 Vyktor 的答案。在子句之外,您可以使用 sys.exc_info() 。另请参阅 traceback 模块一些有用的工具。

 >>> import sys,traceback 
>>> def raise_exception():
... try:
... raise异常
...除了异常:
... ex_type,ex,tb = sys.exc_info )
... traceback.print_tb(tb)
... finally:
... del tb
...
>>> raise_exception()
文件< stdin>,第3行,raise_exception

或在你的情况下(因为你不能修改 try / except 块):

 code>>>> def view_traceback():
... ex_type,ex,tb = sys.exc_info()
... traceback.print_tb(tb)
... del tb
。 ..
>>>尝试:
... raise异常
...除了异常:
... view_traceback()
...
文件< stdin>,第2行,在< module>

要详细说明, sys.exc_info 返回异常,异常类型和traceback,正在处理任何异常。



但是正如您的编辑所示,如果您的异常未被处理,您将试图获得将被打印的追溯它已经处理了。这是一个更难的问题。 正常异常不存储追溯信息,这可能是因为保留异常轻量级允许在发生异常时更快的执行。 (另外,如 ecatmur 所示,在本地变量中存储追溯创建循环引用。)不幸的是,当没有异常处理时,sys.exc_info 返回(无,无,无)其他相关的 sys attribues也没有帮助。在不处理异常的情况下,不建议使用&code> sys.exc_traceback sys.last_traceback 似乎是完美的,但我相信仅在交互式会话中定义。



如果可以控制例外情况,您可能可以使用 inspect 自定义异常存储一些信息。但我甚至不知道该如何工作。



我会补充说,捕获并返回异常是一件不寻常的事情;我建议重构。


Given an Exception object (of unknown origin) is there way to obtain its traceback? I have code like this:

def stuff():
   try:
       .....
       return useful
   except Exception as e:
       return e

result = stuff()
if isinstance(result, Exception):
    result.traceback <-- How?

How can I extract the traceback from the Exception object once I have it?

解决方案

The traceback is not stored in the exception. (Not, that is, in Python 2; see Vyktor's answer for more about the situation in Python 3). Within an except clause, you can retrieve it using sys.exc_info(). See also the traceback module for a few useful tools.

>>> import sys, traceback
>>> def raise_exception():
...     try:
...         raise Exception
...     except Exception:
...         ex_type, ex, tb = sys.exc_info()
...         traceback.print_tb(tb)
...     finally:
...         del tb
... 
>>> raise_exception()
  File "<stdin>", line 3, in raise_exception

Or, in your case (since you can't modify the try/except block):

>>> def view_traceback():
...     ex_type, ex, tb = sys.exc_info()
...     traceback.print_tb(tb)
...     del tb
... 
>>> try:
...     raise Exception
... except Exception:
...     view_traceback()
... 
  File "<stdin>", line 2, in <module>

To elaborate, sys.exc_info returns the exception, exception type, and traceback for whatever exception is currently being handled.

But as your edit indicates, you're trying to get the traceback that would have been printed if your exception had not been handled, after it has already been handled. That's a much harder question. "Normal" exceptions don't store traceback information, perhaps because keeping exceptions lightweight allows for faster execution when an exception does occur. (Also, as ecatmur observes, storing tracebacks in local variables creates circular references.) And unfortunately, sys.exc_info returns (None, None, None) when no exception is being handled. Other related sys attribues don't help either. sys.exc_traceback is deprecated and undefined when no exception is being handled; sys.last_traceback seems perfect, but I believe is only defined in interactive sessions.

If you can control how the exception is raised, you might be able to use inspect and a custom exception to store some of the information. But I'm not even sure how that would work.

I'll add that catching and returning an exception is kind of an unusual thing to do; I would suggest refactoring.

这篇关于从异常对象提取回溯信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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