Exception message (Python 2.6)

This prints the exception message:

except Exception, e:
    print "Couldn't do it: %s" % e

This will show the whole traceback:

import traceback

# ...

except Exception, e:
    traceback.print_exc()

But you might not want to catch Exception. The narrower you can make your catch, the better, generally. So you might want to try:

except IOError, e:

instead. Also on the subject of narrowing your exception handling, if you are only concerned about missing files, then put the try-except only around the open:

try:
    pkl_file = open('monitor.dat', 'rb')
except IOError, e:
    print 'No such file or directory: %s' % e

monitoring_pickle = pickle.load(pkl_file)
pkl_file.close()

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)