Skip to content

Instantly share code, notes, and snippets.

@poros
Created October 4, 2015 14:04
Show Gist options
  • Save poros/1a2f28dba06c76639804 to your computer and use it in GitHub Desktop.
Save poros/1a2f28dba06c76639804 to your computer and use it in GitHub Desktop.
Complete exception handling
try:
f = open(filename, 'r')
except IOError as e:
print 'cannot open ', filename
print "I/O error({0}): {1}".format(e.errno, e.strerror)
except:
log.exception("Unexpected error")
# print "Unexpected error:", sys.exc_info()[0]
raise
else:
print 'read', len(f.readlines()), 'lines'
finally:
print "executing finally clause"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment