32

I want to keep & use the error value of an exception in both Python 2.5, 2.7 and 3.2.

In Python 2.5 and 2.7 (but not 3.x), this works:

try:
    print(10 * (1/0))
except ZeroDivisionError,  error:       # old skool
    print("Yep, error caught:", error)

In Python 2.7 and 3.2 (but not in 2.5), this works:

try:
    print(10 * (1/0))
except (ZeroDivisionError) as error:    # 'as' is needed by Python 3
    print("Yep, error caught:", error)

Is there any code for this purpose that works in both 2.5, 2.7 and 3.2?

Thanks

9
  • Can you not upgrade your 2.5 code to at least 2.7 and preferably 3? Commented Jul 1, 2012 at 20:35
  • except (ZeroDivisionError) as error: works fine in python 2.7 ideone.com/KfHBs Commented Jul 1, 2012 at 20:35
  • 2
    Python 3 is expressly not backwards compatible. Why restrict yourself to a limited subset of syntax that both languages support? Commented Jul 1, 2012 at 20:40
  • @Ben (and Cameron) The code is used on appliances (often with Python 2.5) and on Linux installation (with Python 3.x on the horizon). Therefore both 2.5 and 3.x should work .... If I must choose, it will be 2.x code; that will work on on all installations: appliances and Linux installations. Commented Jul 1, 2012 at 20:48
  • 2
    @AshwiniChaudhary Indeed. That's what I already said in my post. Python 2.7 looks like a fine hybrid: accepting both 2.x and 3.x style python code. It's about 2.5 versus 3.x ... Commented Jul 1, 2012 at 20:49

1 Answer 1

41

You can use one code base on Pythons 2.5 through 3.2, but it isn't easy. You can take a look at coverage.py, which runs on 2.3 through 3.3 with a single code base.

The way to catch an exception and get a reference to the exception that works in all of them is this:

except ValueError:
    _, err, _ = sys.exc_info()
    #.. use err...

This is equivalent to:

except ValueError as err:
    #.. use err...
Sign up to request clarification or add additional context in comments.

8 Comments

Granted it works, but maybe the cure is worse (uglier) than the problem. Another way would be using "as" and a code conversion script when packaging code for 2.5. YMMV.
The problem is that python does significant and unnecesary changes to its syntax between versions. Imagine this in C: from version x.y you use "@" instead of "{" :) This is plain stupid and the cure is much better - after all we all want it to just work!
@Terminus Think of changes from Python 2 to Python 3 as analogous to the change in syntax of function declarations from first edition K&R C to ANSI C. C89/C90 accepts both syntaxes, as Python 2.7 does.
The difference is that no one uses K&R now and no one used it shortly after introduction of ANSI. Python 3 was introduced years and years ago and people still use 2.7. So, the solution presented here is excellent - it supports all commonly used versions. And if it's ugly? That's not my problem, i want it to WORK.
Unfortunately for the purposes of this answer, the coverage.py link has since been updated to python2.6 and later syntax. This is a link to an older commit: bitbucket.org/ned/coveragepy/src/…
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.