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


Groups > comp.lang.python > #12241

Re: Catch and name an exception in Python 2.5 +

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <t@jollybox.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.013
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; '2.7': 0.04; 'subject:Python': 0.06; 'python': 0.08; 'bind': 0.09; 'elegant,': 0.09; 'error:': 0.12; 'exception': 0.12; 'case.': 0.15; '"as': 0.16; 'error"': 0.16; 'wrote:': 0.16; "haven't": 0.20; 'header:In- Reply-To:1': 0.22; 'versions': 0.23; '2.5,': 0.23; "i'm": 0.27; 'pass': 0.29; 'work:': 0.29; 'error': 0.32; 'actually': 0.33; 'there': 0.33; 'to:addr:python-list': 0.33; '...': 0.34; 'header :User-Agent:1': 0.34; '2.6': 0.34; 'try:': 0.34; 'pretty': 0.35; 'but': 0.37; 'steven': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'received:192': 0.39; 'either': 0.39; 'to:addr:python.org': 0.39; "i'd": 0.40; "it's": 0.40; 'subject:name': 0.67; 'received:62': 0.70; 'from:addr:t': 0.84; 'subject:Catch': 0.91; 'subject: +': 0.93
Date Fri, 26 Aug 2011 22:36:21 +0200
From Thomas Jollans <t@jollybox.de>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:6.0) Gecko/20110816 Thunderbird/6.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Catch and name an exception in Python 2.5 +
References <4e57fa5c$0$29968$c3e8da3$5496439d@news.astraweb.com>
In-Reply-To <4e57fa5c$0$29968$c3e8da3$5496439d@news.astraweb.com>
X-Enigmail-Version 1.3.1
OpenPGP id=5C8691ED
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.452.1314390991.27778.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1314390991 news.xs4all.nl 2523 [2001:888:2000:d::a6]:55674
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12241

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web