catching all exceptions

Discussion in 'Python' started by a_geek@web.de, Aug 13, 2005.

  1. Guest

    Hello,

    I'd like to catch all exeptions and be able to inspect them.

    The simple case: I know which exceptions I'll get:

    # standard textbook example:
    try:
    something()
    except ThisException, e:
    print "some error occurred: ", str(e)


    The not-so-simple case: Handling all other exceptions:

    # nice-to-have:
    try:
    something()
    except *, e:
    print "some error occurred: ", type(e), str(e)


    Well, actually the second statement doesn't even compile... any ideas
    why I shouldn't be able to catch "anonymous" exceptions like this, or
    whether and how I can (and only overlooked it)?


    TIA!


    Kind Regards,
    Toni
     
    , Aug 13, 2005
    #1
    1. Advertising

  2. Paolino Guest

    wrote:
    > Hello,
    >
    > I'd like to catch all exeptions and be able to inspect them.
    >
    > The simple case: I know which exceptions I'll get:
    >
    > # standard textbook example:
    > try:
    > something()
    > except ThisException, e:
    > print "some error occurred: ", str(e)
    >
    >
    > The not-so-simple case: Handling all other exceptions:
    >
    > # nice-to-have:
    > try:
    > something()
    > except *, e:
    > print "some error occurred: ", type(e), str(e)
    >
    >

    except Exception:# catch them all.

    Then use moudule 'traceback' to inspect

    Paolino





    ___________________________________
    Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB
    http://mail.yahoo.it
     
    Paolino, Aug 13, 2005
    #2
    1. Advertising

  3. On Sat, 13 Aug 2005 17:42:00 +0200, wrote:

    >Hello,
    >
    >I'd like to catch all exeptions and be able to inspect them.
    >
    >The simple case: I know which exceptions I'll get:
    >
    ># standard textbook example:
    >try:
    > something()
    >except ThisException, e:
    > print "some error occurred: ", str(e)
    >
    >
    >The not-so-simple case: Handling all other exceptions:
    >
    ># nice-to-have:
    >try:
    > something()
    >except *, e:
    > print "some error occurred: ", type(e), str(e)
    >
    >
    >Well, actually the second statement doesn't even compile... any ideas
    >why I shouldn't be able to catch "anonymous" exceptions like this, or
    >whether and how I can (and only overlooked it)?
    >
    >
    >TIA!
    >


    >>> def test(something):

    ... try:
    ... something()
    ... except Exception, e:
    ... print '%s: %s'% (e.__class__.__name__, e)
    ...
    >>> test(lambda: 1/0)

    ZeroDivisionError: integer division or modulo by zero
    >>> test(lambda: unk)

    NameError: global name 'unk' is not defined
    >>> test(lambda: open('unk'))

    IOError: [Errno 2] No such file or directory: 'unk'

    All exceptions should derive from Exception, so the above should catch them,
    although there are deprecated string exceptions that will not be caught.
    You can catch these with a bare except, but it is probably better not to,
    so you will know there's something to clean up.

    If you do have to deal with them, you can then catch them by name individually,
    or all with the bare except, e.g.,

    >>> def strexraiser(s): raise s

    ...
    >>> def test(something):

    ... try:
    ... something()
    ... except Exception, e:
    ... print '%s: %s'% (e.__class__.__name__, e)
    ... except 'ugh':
    ... print 'Caught "ugh"'
    ... except:
    ... print sys.exc_info()
    ...
    >>> import sys # forgot, will need when above executes ;-)
    >>>
    >>> test(lambda:strexraiser('ugh'))

    Caught "ugh"
    >>> test(lambda:strexraiser('gak'))

    ('gak', None, <traceback object at 0x02EF0ACC>)
    >>> test(lambda:1/0)

    ZeroDivisionError: integer division or modulo by zero

    Regards,
    Bengt Richter
     
    Bengt Richter, Aug 13, 2005
    #3
  4. > Hello,
    >
    > I'd like to catch all exeptions and be able to inspect them.
    >
    > The simple case: I know which exceptions I'll get:
    >
    > # standard textbook example:
    > try:
    > something()
    > except ThisException, e:
    > print "some error occurred: ", str(e)
    >
    >
    > The not-so-simple case: Handling all other exceptions:
    >
    > # nice-to-have:
    > try:
    > something()
    > except *, e:
    > print "some error occurred: ", type(e), str(e)
    >
    >
    > Well, actually the second statement doesn't even compile... any ideas
    > why I shouldn't be able to catch "anonymous" exceptions like this, or
    > whether and how I can (and only overlooked it)?
    >
    >
    > TIA!
    >
    >
    > Kind Regards,
    > Toni


    Try this:

    import sys

    try:
    something()
    except:
    info = sys.exc_info()
    ...

    and you can inspect the tuple info, which contains the exception type,
    value, and traceback.

    Best regards,
    Tom
     
    Tomasz Lisowski, Aug 13, 2005
    #4
  5. Guest

    Re: catching all exceptions [SOLVED]

    Hello,

    Tomasz Lisowski wrote:
    >> Well, actually the second statement doesn't even compile... any ideas
    >> why I shouldn't be able to catch "anonymous" exceptions like this, or
    >> whether and how I can (and only overlooked it)?


    [ one of three suggestions: ]

    > Try this:


    Ok, so the answer simply was that I didn't see "it", although the
    solution is in the manual.

    Thank you!


    Kind Regards,
    Toni
     
    , Aug 13, 2005
    #5
    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. Marina
    Replies:
    2
    Views:
    481
    Marina
    Jul 8, 2003
  2. Amil Hanish
    Replies:
    0
    Views:
    555
    Amil Hanish
    Apr 13, 2006
  3. Adam Maass
    Replies:
    5
    Views:
    410
    Sudsy
    Jul 22, 2003
  4. Mike Schilling
    Replies:
    2
    Views:
    353
    Mike Schilling
    Jul 16, 2003
  5. Mick
    Replies:
    0
    Views:
    438
Loading...

Share This Page