Difference between revisions of "Python - Exception Handling"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) (New page: =Overview= <pre> try: out = open(data_file, 'w') except: print "XXX" sys.exit(0) </pre> Category:Python) |
PeterHarding (talk | contribs) |
||
Line 1: | Line 1: | ||
=Overview= | =Overview= | ||
=Examples= | |||
<pre> | <pre> | ||
Line 10: | Line 11: | ||
</pre> | </pre> | ||
<pre> | |||
try: | |||
pf = open(pickle_file, "r") | |||
except IOError, msg: | |||
sys.stderr.write('%s: cannot open: %s\n' % (pickle_file, msg)) | |||
details = defaults | |||
return | |||
</pre> | |||
<pre> | |||
try: | |||
ifd = open(source_fname, 'r') | |||
except IOError, e: | |||
sys.stderr.write('[scramble] Open failed: ' + str(e) + '\n') | |||
sys.exit(1) | |||
</pre> | |||
<pre> | |||
try: | |||
sys.exit(main()) | |||
except KeyboardInterrupt, e: | |||
print "[skel] Interrupted!" | |||
</pre> | |||
<pre> | |||
try: | |||
opts, args = getopt.getopt(sys.argv[1:], "dD:i:s:vV?") | |||
except getopt.error, msg: | |||
print __doc__ | |||
return 1 | |||
</pre> | |||
<pre> | |||
</pre> | |||
=Exceptions= | |||
* IOError | |||
* ValueError | |||
* KeyError | |||
[[Category:Python]] | [[Category:Python]] |
Revision as of 10:52, 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('%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
Exceptions
- IOError
- ValueError
- KeyError