Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12240 > unrolled thread

Catch and name an exception in Python 2.5 +

Started bySteven D'Aprano <steve+comp.lang.python@pearwood.info>
First post2011-08-27 05:56 +1000
Last post2011-08-27 11:57 +0200
Articles 4 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Catch and name an exception in Python 2.5 + Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-27 05:56 +1000
    Re: Catch and name an exception in Python 2.5 + Thomas Jollans <t@jollybox.de> - 2011-08-26 22:36 +0200
      Re: Catch and name an exception in Python 2.5 + Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-27 13:45 +1000
        Re: Catch and name an exception in Python 2.5 + Thomas Jollans <t@jollybox.de> - 2011-08-27 11:57 +0200

#12240 — Catch and name an exception in Python 2.5 +

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-08-27 05:56 +1000
SubjectCatch and name an exception in Python 2.5 +
Message-ID<4e57fa5c$0$29968$c3e8da3$5496439d@news.astraweb.com>
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

[toc] | [next] | [standalone]


#12241

FromThomas Jollans <t@jollybox.de>
Date2011-08-26 22:36 +0200
Message-ID<mailman.452.1314390991.27778.python-list@python.org>
In reply to#12240
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:
> 
> 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.

It's not elegant, and I haven't actually tested this, but this should work:

try:
    ...
except (ValueError, KeyError):
    error = sys.exc_info()[2]

-- 
Thomas

[toc] | [prev] | [next] | [standalone]


#12248

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-08-27 13:45 +1000
Message-ID<4e586858$0$29991$c3e8da3$5496439d@news.astraweb.com>
In reply to#12241
Thomas Jollans wrote:

> On 26/08/11 21:56, Steven D'Aprano wrote:

>> 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:
> 
> try:
>     ...
> except (ValueError, KeyError):
>     error = sys.exc_info()[2]

Great! Thanks for that, except I think you want to use [1], not [2].



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#12257

FromThomas Jollans <t@jollybox.de>
Date2011-08-27 11:57 +0200
Message-ID<mailman.458.1314439072.27778.python-list@python.org>
In reply to#12248
On 27/08/11 05:45, Steven D'Aprano wrote:
> Thomas Jollans wrote:
> 
>> On 26/08/11 21:56, Steven D'Aprano wrote:
> 
>>> 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:
>>
>> try:
>>     ...
>> except (ValueError, KeyError):
>>     error = sys.exc_info()[2]
> 
> Great! Thanks for that, except I think you want to use [1], not [2].

Ah, yes. Of course.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web