Python - Exception Handling
Revision as of 10:52, 29 May 2008 by PeterHarding (talk | contribs)
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