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


Groups > comp.lang.python > #15787

Re: try - except. How to identify errors unknown in advance?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'desirable.': 0.07; 'exception,': 0.07; 'python': 0.08; 'from:addr:python': 0.09; 'am,': 0.12; 'exception': 0.12; '16,': 0.15; 'except:': 0.16; 'frederic': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'omitting': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'reply-to:addr:python-list': 0.16; 'row': 0.16; 'wed,': 0.17; 'advance': 0.17; 'wrote:': 0.18; "doesn't": 0.23; 'header:In-Reply-To:1': 0.23; "i'm": 0.26; 'all,': 0.27; 'print': 0.29; 'generally': 0.30; 'bare': 0.30; 'chris': 0.30; 'subject:?': 0.31; 'error': 0.31; 'nov': 0.31; 'to:addr:python-list': 0.32; 'header:User-Agent:1': 0.33; 'match': 0.33; 'received:84': 0.34; 'probably': 0.34; 'reply- to:addr:python.org': 0.34; 'try:': 0.34; 'subject:How': 0.35; 'something': 0.36; 'but': 0.37; "i'd": 0.39; 'should': 0.39; 'except': 0.39; "there's": 0.39; "it's": 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.39; '2011': 0.62; 'subject:. ': 0.65; 'header:Reply-To:1': 0.70; 'reply-to:no real name:2**0': 0.71; 'contrary': 0.73; 'everything,': 0.84; 'ridiculously': 0.84; 'subject:try': 0.84
X-CM-Score 0.00
X-CNFS-Analysis v=2.0 cv=crMZYiEi c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=9jsOeB20M3cA:10 a=blGdz3jhaGsA:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=4ToGEpBjysMhq4K5xs0A:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117
X-AUTH mrabarnett:2500
Date Wed, 16 Nov 2011 20:21:16 +0000
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20111105 Thunderbird/8.0
MIME-Version 1.0
To python-list@python.org
Subject Re: try - except. How to identify errors unknown in advance?
References <1321462647.2315.31.camel@hatchbox-one> <CALvWhxtY+gaC+xHx4qdakAMstFkOC8eUxRiV9tBGRO3Q46QqXw@mail.gmail.com>
In-Reply-To <CALvWhxtY+gaC+xHx4qdakAMstFkOC8eUxRiV9tBGRO3Q46QqXw@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To python-list@python.org
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.2781.1321474855.27778.python-list@python.org> (permalink)
Lines 36
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1321474855 news.xs4all.nl 6899 [2001:888:2000:d::a6]:53451
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:15787

Show key headers only | View raw


On 16/11/2011 17:09, Chris Kaynor wrote:
> On Wed, Nov 16, 2011 at 8:57 AM, Frederic Rentsch
> <anthra.norell@bluewin.ch>  wrote:
>> Hi all,
>>
>>
>> I'd like to log MySQL errors. If I do:
>>
>>         try: (command)
>>         except MySQLdb.OperationalError, e: print e
>>
>> I may get something like:
>>
>>         (1136, "Column count doesn't match value count at row 1")
>>
>> If I don't know in advance which error to expect, but on the contrary
>> want to find out which error occurred, I can catch any error by omitting
>> the name:
>>
>>         except: (handle)
>>
>> But now I don't have access to the error message 'e'. I'm sure there's a
>> way and it's probably ridiculously simple.
>
> except Exception, e: (or, in Py3, except Exception as e is prefereed).
>
In Python 3, "except Exception as e" is not just preferred: it's the
only form.

> Note that you should generally avoid bare except statements "except:"
> as that will catch everything, including KeyboardInterrupt and
> SystemExit which may not be desirable.
>
[snip]

Very true.

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


Thread

Re: try - except. How to identify errors unknown in advance? MRAB <python@mrabarnett.plus.com> - 2011-11-16 20:21 +0000

csiph-web