Python 使用';从';语法

Python 使用';从';语法,python,python-3.x,error-handling,Python,Python 3.x,Error Handling,给定文件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 tr

给定文件
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
具有完整错误链的回溯和连接行的回溯(
回溯(最近一次调用):
上述异常是以下异常的直接原因:
和每个错误的字符串表示

理想情况下,我希望捕获这些输出行以将它们定向到其他位置(例如记录器)

我的一个解决方案是迭代
error.\uuuu context\uuuu
并手动添加连词:

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__
这是可行的,但它是有漏洞的,我更喜欢使用一些已经处理了这个问题的标准库模块。

您想使用这个函数:

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
$python3/tmp/a.py
回溯(最近一次呼叫最后一次):
文件“/tmp/a.py”,第13行,在
提出我的错误(“发生了特定的事情”)
迈罗:发生了一些特别的事情
上述异常是以下异常的直接原因:
回溯(最近一次呼叫最后一次):
文件“/tmp/a.py”,第15行,在
从错误中引发MySecondError(“发生了一般性的事情”)
迈斯康德罗:发生了一些一般性的事情

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

谢谢,在浏览
回溯
模块文档时应该会发现这一点
$ 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