Difference between revisions of "Python - Exception Handling"

From PeformIQ Upgrade
Jump to navigation Jump to search
Line 15: Line 15:
       pf = open(pickle_file, "r")
       pf = open(pickle_file, "r")
   except IOError, msg:
   except IOError, msg:
       sys.stderr.write('%s: cannot open: %s\n' % (pickle_file, msg))
       sys.stderr.write('[XXX]  %s - Cannot open: %s\n' % (pickle_file, msg))
       details = defaults
       details = defaults
       return
       return
Line 49: Line 49:
=Exceptions=
=Exceptions=


* AttributeError
* IOError
* IOError
* ValueError
* ValueError

Revision as of 10:54, 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


Exceptions

  • AttributeError
  • IOError
  • ValueError
  • KeyError