[英]Full traceback for chained exceptions when using 'raise from' syntax
[英]Get only last traceback without using raise from None?
我的项目中有一个常见的情况,我使用自定义异常来包装内置的异常场景,例如:
# meaningless here but in other situations its useful...
try:
5/0
except ZeroDivisionError as e:
raise MyCustomException(msg=str(e))
以及看起来像这样的通用异常处理程序:
@app.errorhandler(Exception) # this decorator is flask related - ignore it
def handle_error(error):
if isinstance(error, MyCustomException):
# some code
# I only want MyCustomException traceback
else:
# some other code
exc_stack = traceback.format_exc(limit=5)
这里的已知问题是我得到两个异常回溯,而在if-case中我只想要最后一个。
据我所知,这个问题有两个解决方案。
第一种解决方法(使用from None
):
try:
5/0
except ZeroDivisionError as e:
raise MyCustomException(msg=str(e)) from None # python3
第二种解决方法(在引发第二个异常之前调用回溯)
try:
5/0
except ZeroDivisionError as e:
tracb = traceback.format_exc(limit=5)
raise MyCustomException(msg=str(e), tracb_msg=tracb)
无需在异常处理程序中调用traceback.format_exc()
,只需使用传递给实例的tracb_msg
即可。 显然,第一种解决方法更简单。
我的问题:
每次我提出MyCustomException
时 ,这两种方法都会在代码中重复出现(重复代码/技巧)数十次。 有没有人想出一招给处理函数里面,一旦处理呢?
使用__suppress_context__
属性禁用上下文打印。
根据文档 ,使用raise MyCustomException(foo) from bar
设置__cause__
到bar
,并使用__context__
到原始异常(隐式链接异常)。
仅当
__cause__
为None且__suppress_context__
为false时,__suppress_context__
显示__context__
的隐式链接异常。
这是一个例子:
# Declare an exception that never shows context.
class MyCustomException(Exception):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__suppress_context__ = True
try:
1/0
except ZeroDivisionError as e:
raise MyCustomException(str(e))
这是我得到的输出:
Traceback (most recent call last):
File "/home/don/workspace/scratch/scratch.py", line 12, in <module>
raise MyCustomException(str(e))
MyCustomException: division by zero
如果我将__suppress_context__
设置为False
这是输出:
Traceback (most recent call last):
File "/home/don/workspace/scratch/scratch.py", line 10, in <module>
1/0
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/don/workspace/scratch/scratch.py", line 12, in <module>
raise MyCustomException(str(e))
MyCustomException: division by zero
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.