Skip to content

Instantly share code, notes, and snippets.

@pariekshit
Created March 20, 2019 15:52
Show Gist options
  • Save pariekshit/371d90a52daeac83b0c0ce86d620fbbb to your computer and use it in GitHub Desktop.
Save pariekshit/371d90a52daeac83b0c0ce86d620fbbb to your computer and use it in GitHub Desktop.
Retrieve the last python traceback
>>> import sys
>>> import traceback
>>> 
>>> def _get_traceback():
...     exc_type, exc_value, exc_traceback = sys.exc_info()
...     return traceback.format_exception(exc_type, exc_value, exc_traceback)
... 
>>> try:
...     raise Exception("this is an exception")
... except:
...     print(_get_traceback())
...     
... 
['Traceback (most recent call last):\n', '  File "<bpython-input-14>", line 2, in <module>\n    raise Exception("this is an exception")\n', 'Exception: this is an exception\n']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment