Difference between revisions of "Python - Exception Handling"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) |
PeterHarding (talk | contribs) |
||
| Line 44: | Line 44: | ||
<pre> | <pre> | ||
try: | |||
... | |||
except: | |||
... | |||
finally: | |||
os.chdir(root) | |||
</pre> | </pre> | ||
=Exceptions= | =Exceptions= | ||
Here are some common exception values: | |||
* AttributeError | * AttributeError | ||
* IOError | * IOError | ||
* KeyError | |||
* KeyboardInterrupt | |||
* ValueError | * ValueError | ||
[[Category:Python]] | [[Category:Python]] | ||
Revision as of 10:58, 29 May 2008
Overview
Examples
try:
out = open(data_file, 'w')
except:
print "XXX"
sys.exit(0)
try:
pf = open(pickle_file, "r")
except IOError, msg:
sys.stderr.write('[XXX] %s - Cannot open: %s\n' % (pickle_file, msg))
details = defaults
return
try:
ifd = open(source_fname, 'r')
except IOError, e:
sys.stderr.write('[scramble] Open failed: ' + str(e) + '\n')
sys.exit(1)
try:
sys.exit(main())
except KeyboardInterrupt, e:
print "[skel] Interrupted!"
try:
opts, args = getopt.getopt(sys.argv[1:], "dD:i:s:vV?")
except getopt.error, msg:
print __doc__
return 1
try:
...
except:
...
finally:
os.chdir(root)
Exceptions
Here are some common exception values:
- AttributeError
- IOError
- KeyError
- KeyboardInterrupt
- ValueError