Catching Exceptions.

Discussion in 'Python' started by Keith Bolton, Sep 27, 2004.

  1. Keith Bolton

    Keith Bolton Guest

    I am handling exceptions currently using try, except. Generally I don't
    handle specific exceptions and am catching all.
    Then if an exception occurs, I would like to capture that error string.
    However, in the documentation it seems like
    there is not a way to get the extra str data if you are handling all
    exceptions and not specifically. Is there?

    thanks

    -keith
    Keith Bolton, Sep 27, 2004
    #1
    1. Advertising

  2. Keith Bolton

    Peter Hansen Guest

    Keith Bolton wrote:
    > I am handling exceptions currently using try, except. Generally I don't
    > handle specific exceptions and am catching all.
    > Then if an exception occurs, I would like to capture that error string.
    > However, in the documentation it seems like
    > there is not a way to get the extra str data if you are handling all
    > exceptions and not specifically. Is there?


    Do generic "try/except" is a *very* bad idea, and almost never
    either required, or hard to avoid.

    Nevertheless, if you are doing it, you can basically just
    do this to get the extra info:

    try:
    # failing code
    except Exception, ex:
    # use ex here just as you would have in a more specific handler
    # such as with str(ex)

    The key is that all exceptions are descendents of the class
    Exception, except *deprecated* exceptions that are just
    strings (as in "raise 'somefailure'"). (And those should never
    be used in your own code anyway.)

    -Peter
    Peter Hansen, Sep 27, 2004
    #2
    1. Advertising

  3. Keith Bolton

    Fuzzyman Guest

    On Mon, 27 Sep 2004 08:44:52 -0700, "Keith Bolton"
    <> wrote:

    >I am handling exceptions currently using try, except. Generally I don't
    >handle specific exceptions and am catching all.
    >Then if an exception occurs, I would like to capture that error string.
    > However, in the documentation it seems like
    >there is not a way to get the extra str data if you are handling all
    >exceptions and not specifically. Is there?
    >
    >thanks
    >
    >-keith


    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/259180

    Above link is a simple recipe that will just extract the text of an
    exception.

    HTH

    Regards,


    Fuzzy

    http://www.voidspace.org.uk/atlantibits/pythonutils.html
    Fuzzyman, Sep 27, 2004
    #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. Marina
    Replies:
    2
    Views:
    475
    Marina
    Jul 8, 2003
  2. Amil Hanish
    Replies:
    0
    Views:
    551
    Amil Hanish
    Apr 13, 2006
  3. Adam Maass
    Replies:
    5
    Views:
    403
    Sudsy
    Jul 22, 2003
  4. Mike Schilling
    Replies:
    2
    Views:
    351
    Mike Schilling
    Jul 16, 2003
  5. Mick
    Replies:
    0
    Views:
    430
Loading...

Share This Page