How to Catch 2 Exceptions at once?

Discussion in 'Python' started by =?ISO-8859-1?Q?Gregory_Pi=F1ero?=, Oct 1, 2006.

  1. How can I catch 2 exceptions at once for example:

    try:
    self.gses = opener.open(req)
    except (urllib2.HTTPError,urllib2.URLError):
    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)
    =?ISO-8859-1?Q?Gregory_Pi=F1ero?=, Oct 1, 2006
    #1
    1. Advertising

  2. =?ISO-8859-1?Q?Gregory_Pi=F1ero?=

    James Stroud Guest

    Gregory Piñero wrote:
    > How can I catch 2 exceptions at once for example:
    >
    > try:
    > self.gses = opener.open(req)
    > except (urllib2.HTTPError,urllib2.URLError):
    > do something..
    >
    > Seems to work, but how do I also get information about the error?
    >

    py> try:
    .... 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

    http://www.jamesstroud.com/
    James Stroud, Oct 1, 2006
    #2
    1. Advertising

  3. =?ISO-8859-1?Q?Gregory_Pi=F1ero?=

    John Machin Guest

    Gregory Piñero wrote:
    > How can I catch 2 exceptions at once for example:
    >
    > try:
    > self.gses = opener.open(req)
    > except (urllib2.HTTPError,urllib2.URLError):
    > 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.html#SECTION0010300000000000000000):

    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, KeyboardInterrupt, ZeroDivisionError), e :
    print repr(e)
    print e
    print dir(e)
    print e.args
    print e.__class__.__name__
    except:
    print "Unexpected error:", sys.exc_info()[0]
    # no example shown; read the fine manual :)
    raise

    C:\junk>python gregpexc.py
    File name:<exceptions.KeyboardInterrupt instance at 0x00AF1EE0>

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

    C:\junk>python gregpexc.py
    File name:kl;lklklklk
    <exceptions.IOError 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.py
    <exceptions.ZeroDivisionError instance at 0x00AF1F08>
    integer division or modulo by zero
    ['__doc__', '__getitem__', '__init__', '__module__', '__str__', 'args']
    ('integer division or modulo by zero',)
    ZeroDivisionError

    HTH,
    John
    John Machin, Oct 1, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Black
    Replies:
    8
    Views:
    4,152
    Xenos
    Aug 20, 2004
  2. Steve Holden

    Re: How to Catch 2 Exceptions at once?

    Steve Holden, Oct 1, 2006, in forum: Python
    Replies:
    0
    Views:
    362
    Steve Holden
    Oct 1, 2006
  3. Replies:
    6
    Views:
    488
    Stan Kitsis [MSFT]
    Mar 8, 2007
  4. Adam
    Replies:
    9
    Views:
    571
    red floyd
    Feb 2, 2006
  5. Gancy
    Replies:
    4
    Views:
    176
    Rasto Levrinc
    Feb 3, 2005
Loading...

Share This Page