How to format traceback objects in Python

Viewed 27844

I have a traceback object that I want to show in the nice format I get when calling traceback.format_exc().

Is there a builtin function for this? Or a few lines of code?

5 Answers

Couldn't find this anywhere, so I'm posting it here for future people and my future self.

try:
  raise Exception('Not an Exception')
except Exception as err:
  msg = "".join(traceback.format_exception(type(err), err, err.__traceback__))
  print(msg)

This takes your exception and provides a string formatted identically to python's default exception printer/print_tb

Related