catching all exceptions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • a_geek@web.de

    #1

    catching all exceptions

    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
  • Paolino

    #2
    Re: catching all exceptions

    a_geek@web.de wrote:[color=blue]
    > 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)
    >
    >[/color]
    except Exception:# catch them all.

    Then use moudule 'traceback' to inspect

    Paolino





    _______________ _______________ _____
    Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB

    Comment

    • Bengt Richter

      #3
      Re: catching all exceptions

      On Sat, 13 Aug 2005 17:42:00 +0200, a_geek@web.de wrote:
      [color=blue]
      >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!
      >[/color]
      [color=blue][color=green][color=darkred]
      >>> def test(something) :[/color][/color][/color]
      ... try:
      ... something()
      ... except Exception, e:
      ... print '%s: %s'% (e.__class__.__ name__, e)
      ...[color=blue][color=green][color=darkred]
      >>> test(lambda: 1/0)[/color][/color][/color]
      ZeroDivisionErr or: integer division or modulo by zero[color=blue][color=green][color=darkred]
      >>> test(lambda: unk)[/color][/color][/color]
      NameError: global name 'unk' is not defined[color=blue][color=green][color=darkred]
      >>> test(lambda: open('unk'))[/color][/color][/color]
      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.,
      [color=blue][color=green][color=darkred]
      >>> def strexraiser(s): raise s[/color][/color][/color]
      ...[color=blue][color=green][color=darkred]
      >>> def test(something) :[/color][/color][/color]
      ... try:
      ... something()
      ... except Exception, e:
      ... print '%s: %s'% (e.__class__.__ name__, e)
      ... except 'ugh':
      ... print 'Caught "ugh"'
      ... except:
      ... print sys.exc_info()
      ...[color=blue][color=green][color=darkred]
      >>> import sys # forgot, will need when above executes ;-)
      >>>
      >>> test(lambda:str exraiser('ugh') )[/color][/color][/color]
      Caught "ugh"[color=blue][color=green][color=darkred]
      >>> test(lambda:str exraiser('gak') )[/color][/color][/color]
      ('gak', None, <traceback object at 0x02EF0ACC>)[color=blue][color=green][color=darkred]
      >>> test(lambda:1/0)[/color][/color][/color]
      ZeroDivisionErr or: integer division or modulo by zero

      Regards,
      Bengt Richter

      Comment

      • Tomasz Lisowski

        #4
        Re: catching all exceptions

        > Hello,[color=blue]
        >
        > 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[/color]

        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

        Comment

        • a_geek@web.de

          #5
          Re: catching all exceptions [SOLVED]

          Hello,

          Tomasz Lisowski wrote:[color=blue][color=green]
          >> 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)?[/color][/color]

          [ one of three suggestions: ]
          [color=blue]
          > Try this:[/color]

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

          Thank you!


          Kind Regards,
          Toni

          Comment

          Working...