How to Catch 2 Exceptions at once?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gregory Piñero

    #1

    How to Catch 2 Exceptions at once?

    How can I catch 2 exceptions at once for example:

    try:
    self.gses = opener.open(req )
    except (urllib2.HTTPEr ror,urllib2.URL Error):
    do something..

    Seems to work, but how do I also get information about the error?

    --
    Gregory Piñero
    Chief Innovation Officer
    Blended Technologies
    (www.blendedtechnologies.com)
  • James Stroud

    #2
    Re: How to Catch 2 Exceptions at once?

    Gregory Piñero wrote:
    How can I catch 2 exceptions at once for example:
    >
    try:
    self.gses = opener.open(req )
    except (urllib2.HTTPEr ror,urllib2.URL Error):
    do something..
    >
    Seems to work, but how do I also get information about the error?
    >
    pytry:
    .... raise ValueError, 'Illegal value for your shoe size!'
    .... except (IndexError, ValueError), e:
    .... print e
    ....
    Illegal value for your shoe size!


    --
    James Stroud
    UCLA-DOE Institute for Genomics and Proteomics
    Box 951570
    Los Angeles, CA 90095


    Comment

    • John Machin

      #3
      Re: How to Catch 2 Exceptions at once?


      Gregory Piñero wrote:
      How can I catch 2 exceptions at once for example:
      >
      try:
      self.gses = opener.open(req )
      except (urllib2.HTTPEr ror,urllib2.URL Error):
      do something..
      >
      Seems to work, but how do I also get information about the error?
      Errr .. the same way as if you mentioned only one exception. The
      following is an expansion of the scarcely-describable-as-bare coverage
      in the tutorial
      (http://docs.python.org/tut/node10.ht...000000000000):

      C:\junk>cat gregpexc.py
      import sys
      try:
      fname = raw_input('File name:')
      f = open(fname)
      i = 1 / 0
      # except IOError, (errno, strerror):
      # print "I/O error(%s): %s" % (errno, strerror)
      except (IOError, KeyboardInterru pt, ZeroDivisionErr or), e :
      print repr(e)
      print e
      print dir(e)
      print e.args
      print e.__class__.__n ame__
      except:
      print "Unexpected error:", sys.exc_info()[0]
      # no example shown; read the fine manual :-)
      raise

      C:\junk>python gregpexc.py
      File name:<exception s.KeyboardInter rupt instance at 0x00AF1EE0>

      ['__doc__', '__getitem__', '__init__', '__module__', '__str__', 'args']
      ()
      KeyboardInterru pt

      C:\junk>python gregpexc.py
      File name:kl;lklklkl k
      <exceptions.IOE rror instance at 0x00AF1F08>
      [Errno 2] No such file or directory: 'kl;lklklklk'
      ['__doc__', '__getitem__', '__init__', '__module__', '__str__', 'args',
      'errno',
      'filename', 'strerror']
      (2, 'No such file or directory')
      IOError

      C:\junk>python gregpexc.py
      File name:gregpexc.p y
      <exceptions.Zer oDivisionError instance at 0x00AF1F08>
      integer division or modulo by zero
      ['__doc__', '__getitem__', '__init__', '__module__', '__str__', 'args']
      ('integer division or modulo by zero',)
      ZeroDivisionErr or

      HTH,
      John

      Comment

      Working...