Advertisement
here2share

# try_linecache.py

Feb 14th, 2015
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. # try_linecache.py
  2.  
  3. import linecache
  4. import sys
  5.  
  6. def PrintException(err=None):
  7.     exc_type, exc_obj, tb = sys.exc_info()
  8.     f = tb.tb_frame
  9.     lineno = tb.tb_lineno
  10.     filename = f.f_code.co_filename
  11.     linecache.checkcache(filename)
  12.     line = linecache.getline(filename, lineno, f.f_globals)
  13.     if err in [0,None]:
  14.         print 'EXCEPTION IN LINE {} "{}"'.format(lineno, exc_obj)
  15.     elif err == 1:
  16.         print 'EXCEPTION IN LINE ', lineno
  17.     elif err == 2:
  18.         print 'EXCEPTION IN LINE {} "{}"'.format(lineno, line.strip())
  19.     elif err == 3:
  20.         print 'EXCEPTION IN LINE {} "{}": {}'.format(filename, lineno, line.strip(), exc_obj)
  21.     elif err == 4:
  22.         print 'EXCEPTION IN ({}, LINE {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj)
  23.  
  24. try:
  25.     print 1/0
  26. except:
  27.     PrintException()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement