Skip to content

Instantly share code, notes, and snippets.

@matthewoestreich
Created November 14, 2019 01:12
Show Gist options
  • Save matthewoestreich/af843fbf57fe1b4cf8020a119137635d to your computer and use it in GitHub Desktop.
Save matthewoestreich/af843fbf57fe1b4cf8020a119137635d to your computer and use it in GitHub Desktop.
import linecache
import sys
def GetExceptionInfo():
"""
Gets detailed exception info.
------------------------------------------
~~ :EXAMPLE: ~~
------------------------------------------
def ExceptionRaiser():
raise Exception("ERROR")
try:
ExceptionRaiser()
except:
print(GetExceptionInfo())
>>>> EXCEPTION IN (file.py, LINE 01 "ExceptionRaiser()"): ERROR
------------------------------------------
"""
exc_type, exc_obj, tb = sys.exc_info()
f = tb.tb_frame
lineno = tb.tb_lineno
filename = f.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, lineno, f.f_globals)
return 'EXCEPTION IN ({}, LINE {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment