Skip to content

Instantly share code, notes, and snippets.

@PSJoshi
Created February 23, 2015 08:26
Show Gist options
  • Save PSJoshi/3daa4cf5b1f4a4ebd2e4 to your computer and use it in GitHub Desktop.
Save PSJoshi/3daa4cf5b1f4a4ebd2e4 to your computer and use it in GitHub Desktop.
python error handling
try:
raise Exception('blabla')
except Exception:
logging.info('blabla', exc_info=True)
import sys
import traceback
try:
asdfg
except NameError:
exc_type, exc_value, exc_traceback = sys.exc_info()
lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
print ''.join('!! ' + line for line in lines) # Log it or whatever here
This will display:
!! Traceback (most recent call last):
!! File "<stdin>", line 2, in <module>
!! NameError: name 'asdfg' is not defined
try:
asfg
except Exception:
exc_info = sys.exc_info()
logging.info('blabla...- %s'%exc_info)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment