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?

Date 2011-11-16 20:21 +0000
From MRAB <python@mrabarnett.plus.com>
Subject Re: try - except. How to identify errors unknown in advance?
References <1321462647.2315.31.camel@hatchbox-one> <CALvWhxtY+gaC+xHx4qdakAMstFkOC8eUxRiV9tBGRO3Q46QqXw@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2781.1321474855.27778.python-list@python.org> (permalink)

Show all headers | 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