Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'value,': 0.04; 'sys': 0.05; 'desirable.': 0.07; 'exception,': 0.07; 'type,': 0.07; 'command.': 0.09; 'am,': 0.12; 'exception': 0.12; '16,': 0.15; 'except:': 0.16; 'frederic': 0.16; 'omitting': 0.16; 'row': 0.16; 'runtimeerror': 0.16; 'wed,': 0.17; 'advance': 0.17; 'wrote:': 0.18; "doesn't": 0.23; 'header:In-Reply-To:1': 0.23; 'received:74.125.82.174': 0.24; 'received:mail- wy0-f174.google.com': 0.24; 'traceback': 0.24; "i'm": 0.26; 'all,': 0.27; 'raise': 0.27; 'url:mailman': 0.27; 'message- id:@mail.gmail.com': 0.29; 'print': 0.29; 'generally': 0.30; 'bare': 0.30; 'url:library': 0.30; 'subject:?': 0.31; 'error': 0.31; 'nov': 0.31; 'url:listinfo': 0.32; 'to:addr:python-list': 0.32; 'match': 0.33; 'probably': 0.34; 'statement,': 0.34; 'subject:How': 0.35; 'url:python': 0.35; 'something': 0.36; 'received:74.125.82': 0.37; 'but': 0.37; 'received:google.com': 0.38; 'url:org': 0.38; 'url:docs': 0.38; "i'd": 0.39; 'received:74.125': 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; 'contrary': 0.73; 'saving': 0.76; 'everything,': 0.84; 'ridiculously': 0.84; 'subject:try': 0.84 MIME-Version: 1.0 In-Reply-To: <1321462647.2315.31.camel@hatchbox-one> References: <1321462647.2315.31.camel@hatchbox-one> From: Chris Kaynor Date: Wed, 16 Nov 2011 09:09:39 -0800 Subject: Re: try - except. How to identify errors unknown in advance? To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1321463408 news.xs4all.nl 6972 [2001:888:2000:d::a6]:43475 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:15779 On Wed, Nov 16, 2011 at 8:57 AM, Frederic Rentsch wrote: > Hi all, > > > I'd like to log MySQL errors. If I do: > > =A0 =A0 =A0 =A0try: (command) > =A0 =A0 =A0 =A0except MySQLdb.OperationalError, e: print e > > I may get something like: > > =A0 =A0 =A0 =A0(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: > > =A0 =A0 =A0 =A0except: (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). Note that you should generally avoid bare except statements "except:" as that will catch everything, including KeyboardInterrupt and SystemExit which may not be desirable. Even without saving the exception in the except statement, you can get the type, value, and traceback with the sys.exc_info command. See http://docs.python.org/library/sys.html#sys.exc_info For example: py>import sys py>try: py> raise RuntimeError py> except: py> print sys.exc_info() py> (, RuntimeError(), ) > > Frederic > > > > -- > http://mail.python.org/mailman/listinfo/python-list >