Created
February 23, 2015 08:26
-
-
Save PSJoshi/3daa4cf5b1f4a4ebd2e4 to your computer and use it in GitHub Desktop.
python error handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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