首页 > 解决方案 > 使用“raise from”语法时链式异常的完整回溯

问题描述

给定文件errors.py

from traceback import format_tb, extract_tb

class MyError(Exception):
    def __init__(self, message):
        self.message = message

class MySecondError(Exception):
    def __init__(self, message):
        self.message = message

try:
    try:
        raise MyError("Something specific has happened")
    except Exception as error:
        raise MySecondError("Something general has happened") from error
except Exception as error:
    print("".join(format_tb(error.__traceback__)))

运行python errors.py时输出为:

  File "errors.py", line 15, in <module>
    raise MySecondError("Something general has happened") from error

这样做的主要问题是只有“链”中“最高”错误的回溯(MySecondError),并且没有关于包装错误的信息(MyError

如果我删除最终try/except包装器以便不捕获链接错误,我会得到更好的输出:

Traceback (most recent call last):
  File "exceptions.py", line 14, in <module>
    raise MyError("Something specific has happened")
__main__.MyError: Something specific has happened

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "exceptions.py", line 16, in <module>
    raise MySecondError("Something general has happened") from error
__main__.MySecondError: Something general has happened

它具有完整错误链的回溯以及每个错误的连接行 ( Traceback (most recent call last):, The above exception was the direct cause of the following exception:)字符串表示

理想情况下,我想捕获这些输出行以将它们引导到其他地方(例如记录器)

我的一种解决方案是迭代error.__context__并手动添加连接短语:

except Exception as error:
    inner_error = error

    while inner_error:
        if inner_error is not error:
            print("\nThe above exception was the direct cause of the following exception:\n")

        print("Traceback (most recent call last):")
        print("".join(format_tb(inner_error.__traceback__) + [ str(error) ])) 
        inner_error = inner_error.__context__

哪个有效,但它很hacky,我更愿意使用一些已经处理这个的标准库模块。

标签: pythonpython-3.xerror-handling

解决方案


您想使用以下format_exception功能:

from traceback import format_tb, format_exception

class MyError(Exception):
    def __init__(self, message):
        self.message = message

class MySecondError(Exception):
    def __init__(self, message):
        self.message = message

try:
    try:
        raise MyError("Something specific has happened")
    except Exception as error:
        raise MySecondError("Something general has happened") from error
except Exception as error:
    print("".join(format_exception(error.__class__, error, error.__traceback__)))

给出:

$ python3 /tmp/a.py 
Traceback (most recent call last):
  File "/tmp/a.py", line 13, in <module>
    raise MyError("Something specific has happened")
MyError: Something specific has happened

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/tmp/a.py", line 15, in <module>
    raise MySecondError("Something general has happened") from error
MySecondError: Something general has happened

如果您指定chain=False此函数将不会打印联合异常,而只会打印最后一个异常。


推荐阅读