这是一个简化的例子:
class FooException(Exception): pass
class BarException(Exception): pass
def subprocess_one():
# make lots of calls to external service Foo that might fail in all weird ways
pass
def subprocess_two():
# make lots of calls to external service Bar that might fail in all weird ways
raise RuntimeError('hello world')
def api_call():
try:
subprocess_one()
except Exception as e:
raise FooException(e) from e
try:
print('woops')
subprocess_two()
except Exception as e:
print('raising barexception')
raise BarException(e) from e
api_call()
This raises the following stack trace:
Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
woops
raising barexception
Traceback (most recent call last):
File "python", line 23, in <module>
File "python", line 21, in api_call
BarException: hello world
我希望看到更多的内容:
Traceback (most recent call last):
File "python", line 23, in <module>
File "python", line 21, in api_call
File "python", line 9, in subprocess_call
BarException: hello world
或以某种方式以其他方式清楚地显示由包装异常生成的历史跟踪(注意添加原始行和函数作为回溯的一部分)。
我的假设是raise BarException(e) from e
方法可以做到这一点,但是,似乎并没有这样做。没有从exc_info打印堆栈有没有办法做到这一点?
我使用的是Python 3.5+,因此我对Python 2.7解决方案不太感兴趣。