I’m catching exceptions in context managers, however I don’t see all levels of reraised exceptions. Anyone knows how to improve this?
import traceback
def f():
try:
raise Exception("Interesting")
except Exception as e:
raise Exception("Exc {} raised".format(e))
class Try():
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print("Exception {} raised".format(exc_val))
print("".join(traceback.format_tb(exc_tb, 100)))
return True
with Try():
f()
Here I’d like to also see the code line of the “Interesting” exception (line 5) in the traceback, however I get
Exception Exc Interesting raised raised
File "try_test.py", line 19, in <module>
f()
File "try_test.py", line 7, in f
raise Exception("Exc {} raised".format(e))
Use
traceback.format_exceptioninstead oftraceback.format_tb.See the
tracebackdocumentation.