Difference between revisions of "Try/except/finally"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) (New page: category:Python) |
PeterHarding (talk | contribs) |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
=Python Usage= | |||
Some variant examples... | |||
<pre> | |||
try: | |||
pfp = open(pid_path, 'w') | |||
except IOError, e: | |||
sys.stderr.write("[dserver] Open failed - " + str(e) + "\n") | |||
sys.exit(0) | |||
</pre> | |||
Can also use... | |||
<pre> | |||
sys.stderr.write("[dserver] Open failed - " + `e` + "\n") | |||
</pre> | |||
Multiple excepts... | |||
<pre> | |||
try: | |||
pfp = open(pid_path, 'r') | |||
except IOError, (errno, strerror): | |||
pfp = None | |||
# print "I/O error(%s): %s" % (errno, strerror) | |||
except: | |||
print "Unexpected error:", sys.exc_info()[0] | |||
raise | |||
</pre> | |||
[[category:Python]] | [[category:Python]] |
Latest revision as of 13:29, 5 March 2008
Python Usage
Some variant examples...
try: pfp = open(pid_path, 'w') except IOError, e: sys.stderr.write("[dserver] Open failed - " + str(e) + "\n") sys.exit(0)
Can also use...
sys.stderr.write("[dserver] Open failed - " + `e` + "\n")
Multiple excepts...
try: pfp = open(pid_path, 'r') except IOError, (errno, strerror): pfp = None # print "I/O error(%s): %s" % (errno, strerror) except: print "Unexpected error:", sys.exc_info()[0] raise