python exception message capturing
You have to define which type of exception you want to catch. So write except Exception, e:
instead of except, e:
for a general exception (that will be logged anyway).
Other possibility is to write your whole try/except code this way:
try: with open(filepath,'rb') as f: con.storbinary('STOR '+ filepath, f) logger.info('File successfully uploaded to '+ FTPADDR)except Exception, e: # work on python 2.x logger.error('Failed to upload to ftp: '+ str(e))
in Python 3.x and modern versions of Python 2.x use except Exception as e
instead of except Exception, e
:
try: with open(filepath,'rb') as f: con.storbinary('STOR '+ filepath, f) logger.info('File successfully uploaded to '+ FTPADDR)except Exception as e: # work on python 3.x logger.error('Failed to upload to ftp: '+ str(e))
Updating this to something simpler for logger (works for both python 2 and 3). You do not need traceback module.
import logginglogger = logging.Logger('catch_all')def catchEverythingInLog(): try: ... do something ... except Exception as e: logger.error(e, exc_info=True) ... exception handling ...
This is now the old way (though still works):
import sys, tracebackdef catchEverything(): try: ... some operation(s) ... except: exc_type, exc_value, exc_traceback = sys.exc_info() ... exception handling ...
exc_value is the error message.