Skip to content

Instantly share code, notes, and snippets.

@avenet
Created August 27, 2015 20:17
Show Gist options
  • Save avenet/ff84c552127b0547cd56 to your computer and use it in GitHub Desktop.
Save avenet/ff84c552127b0547cd56 to your computer and use it in GitHub Desktop.
Get exception line number
import sys
import traceback
try:
exec(open('error.py').read())
except Exception as e:
exc_type, ex, tb = sys.exc_info()
imported_tb_info = traceback.extract_tb(tb)[-1]
line_number = imported_tb_info[1]
print('{}: Exception in line: {}, message: {}'.format(exc_type.__name__, line_number, ex))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment