我正在尝试创建一个 try- except 块,该块重新引发任意异常,但仅包含回溯堆栈中的最后一个块。
像这样的东西:
import traceback
def my_func(shorten_tracebacks=True):
try:
# Do stuff here
except Exception as e:
if shorten_tracebacks:
raise TheSameTypeOfError, e, traceback.print_exc(limit=1)
else:
raise(e)
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做到这一点?
如果重要的话,我这样做是为了方便调试 jupyter 笔记本中经常使用的某些 API——它们往往会生成非常长的堆栈跟踪,其中只有最后一个块提供信息。这迫使用户频繁滚动。如果你不想缩短回溯,你可以随时设置shorten_tracebacks=False
我的偏好是创建一个没有上下文的新异常(如果异常有上下文,python 将打印该异常和导致该异常的异常,以及导致该异常的异常,依此类推...)
try:
...
except:
raise Exception("Can't ramistat the foo, is foo installed?") from None
Run Code Online (Sandbox Code Playgroud)
一些最佳实践:
这种方法的缺点是,如果您的异常捕获过于广泛,您最终可能会抑制对调试异常非常重要的有用上下文。另一种模式可能如下所示:
try:
...
except Exception as e:
if "ramistat not found" in e.message:
# The ramistat is missing. This is a common kind of error.
# Create a more helpful and shorter message
raise Exception("Can't ramistat the foo, is foo installed?") from None
else:
# Some other kind of problem
raise e
Run Code Online (Sandbox Code Playgroud)
本质上,您检查异常,如果您知道如何处理这种错误,则将其替换为自定义消息。否则,您将重新引发原始异常,并让用户弄清楚该怎么做。
| 归档时间: |
|
| 查看次数: |
685 次 |
| 最近记录: |