【发布时间】:2021-12-03 11:10:05
【问题描述】:
我正在尝试使用自定义 Exception 类并在引发时将其堆栈跟踪作为电子邮件发送。但是__traceback__ 属性是空的。
class TaskFailure(Exception):
def __init__(self, *args: object) -> None:
super().__init__(*args)
self.send_error_report()
def send_error_report(self):
# I want to access the stack trace here
# to send it as email
print(self.__traceback__) # None
raise TaskFailure("could not obtain template")
【问题讨论】:
-
我会在这里问你,但你只期望你的自定义异常的堆栈跟踪吗?
-
运行的时候没有traceback吗?我猜你会的。您试图在初始化中获取
__traceback__值,尽管根据我的理解,只有在您引发异常时才会存在回溯,而且这些都是不同的时刻。这将解释初始化时的空回溯,但之后的正确回溯 -
没错,我在控制台中看到了回溯,但我试图在 Exception 类中访问它以便将其作为电子邮件发送。我猜基于 cmets/answer 是不可能的。
标签: python error-handling