Python - Exception Handling
Revision as of 10:58, 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('[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