Catch and name an exception in Python 2.5 +

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Aug 26 15:56:12 EDT 2011


In Python 3, you can catch an exception and bind it to a name with:

try:
    ...
except ValueError, KeyError as error:
    pass

In Python 2.5, that is written:

try:
    ...
except (ValueError, KeyError), error:
    pass

and the "as error" form gives a SyntaxError.

Python 2.6 and 2.7 accept either form.

Is there any way to catch an exception and bind it to a name which will work
across all Python versions from 2.5 onwards?

I'm pretty sure there isn't, but I thought I'd ask just in case.


-- 
Steven




More information about the Python-list mailing list