Catch and name an exception in Python 2.5 +
t at jollybox.de
Fri Aug 26 22:36:21 CEST 2011
On 26/08/11 21:56, Steven D'Aprano wrote:
> In Python 3, you can catch an exception and bind it to a name with:
> except ValueError, KeyError as error:
> In Python 2.5, that is written:
> except (ValueError, KeyError), error:
> 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.
It's not elegant, and I haven't actually tested this, but this should work:
except (ValueError, KeyError):
error = sys.exc_info()
More information about the Python-list