In a python3 program I have a certain try...except
block where I store exceptions that occur in a certain method into a list
of exceptions that have occurred. A simplified version looks like this:
def the_method(iterable):
errors = []
for i in iterable:
try:
something(i)
except Exception as e:
errors.append(e)
return errors
After the method returns I want to print the errors in the console. How can I print the exceptions with traceback and the usual uncaught exception formatting?
Use the traceback
module. Note that the interface is ancient, so it doesn't know to use type(exc)
and exc.__traceback__
; you'll have to extract those yourself:
for exc in errors:
traceback.print_exception(type(exc), exc, exc.__traceback__)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With