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


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

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

Started byFrederic Rentsch <anthra.norell@bluewin.ch>
First post2011-11-16 19:39 +0100
Last post2011-11-16 19:39 +0100
Articles 1 — 1 participant

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: try - except. How to identify errors unknown in advance? Frederic Rentsch <anthra.norell@bluewin.ch> - 2011-11-16 19:39 +0100

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

FromFrederic Rentsch <anthra.norell@bluewin.ch>
Date2011-11-16 19:39 +0100
SubjectRe: try - except. How to identify errors unknown in advance?
Message-ID<mailman.2779.1321468840.27778.python-list@python.org>
On Wed, 2011-11-16 at 09:09 -0800, 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).
> 
> 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>
> (<type 'exceptions.RuntimeError'>, RuntimeError(), <traceback object
> at 0x0000000002371588>)

Chris, Thanks very much! Great help!

Frederic

[toc] | [standalone]


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


csiph-web