Catching exceptions from Python 2.4 to 3.x

Discussion in 'Python' started by Steven D'Aprano, Nov 17, 2012.

  1. Oh for the day I can drop support for Python 2.4 and 2.5...


    I have some code that needs to run in any version of Python from 2.4
    onwards. Yes, it must be a single code base.

    I wish to catch an exception and bind the exception to a name.

    In Python 2.6 onwards, I can do:

    try:
    something()
    except Exception as err:
    process(err)

    But in 2.4 and 2.5 "as err" gives me a SyntaxError, and I have to write:

    try:
    something()
    except Exception, err:
    process(err)


    Is there some other trick to grab the current exception from inside an
    except block?


    --
    Steven
    Steven D'Aprano, Nov 17, 2012
    #1
    1. Advertising

  2. On 17Nov2012 03:12, Steven D'Aprano <> wrote:
    | Oh for the day I can drop support for Python 2.4 and 2.5...
    |
    |
    | I have some code that needs to run in any version of Python from 2.4
    | onwards. Yes, it must be a single code base.
    |
    | I wish to catch an exception and bind the exception to a name.
    |
    | In Python 2.6 onwards, I can do:
    |
    | try:
    | something()
    | except Exception as err:
    | process(err)
    |
    | But in 2.4 and 2.5 "as err" gives me a SyntaxError, and I have to write:
    |
    | try:
    | something()
    | except Exception, err:
    | process(err)
    |
    |
    | Is there some other trick to grab the current exception from inside an
    | except block?

    sys.exc_info ?
    --
    Cameron Simpson <>

    Peeve: Going to our favorite breakfast place, only to find that they were
    hit by a car...AND WE MISSED IT.
    - Don Baldwin, <>
    Cameron Simpson, Nov 17, 2012
    #2
    1. Advertising

  3. On Sat, 17 Nov 2012 14:26:43 +1100, Cameron Simpson wrote:

    > On 17Nov2012 03:12, Steven D'Aprano
    > <> wrote:


    > | Is there some other trick to grab the current exception from inside an
    > | except block?
    >
    > sys.exc_info ?



    Thanks, that is just what I was looking for.



    --
    Steven
    Steven D'Aprano, Nov 17, 2012
    #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. Senthil Kumar

    Catching Python exceptions in C

    Senthil Kumar, Dec 8, 2008, in forum: Python
    Replies:
    4
    Views:
    1,796
    Ivan Illarionov
    Dec 9, 2008
Loading...

Share This Page