给定一个Exception对象(来源不明)有没有办法获得它的追溯?我有这样的代码:
def stuff():
try:
.....
return useful
except Exception as e:
return e
result = stuff()
if isinstance(result, Exception):
result.traceback <-- How?
Run Code Online (Sandbox Code Playgroud)
有了它,如何从Exception对象中提取回溯?
我有一个生成器和一个消耗它的函数:
def read():
while something():
yield something_else()
def process():
for item in read():
do stuff
Run Code Online (Sandbox Code Playgroud)
如果生成器抛出异常,我想在使用者函数中处理它,然后继续使用迭代器直到它耗尽.请注意,我不希望在生成器中有任何异常处理代码.
我想到了类似的东西:
reader = read()
while True:
try:
item = next(reader)
except StopIteration:
break
except Exception as e:
log error
continue
do_stuff(item)
Run Code Online (Sandbox Code Playgroud)
但这对我来说相当尴尬.