|
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. Frederic -- http://mail.python.org/mailman/listinfo/python-list |
|
On Wed, Nov 16, 2011 at 8:57 AM, Frederic Rentsch
<[hidden email]> 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>) > > Frederic > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list |
|
On Wed, 2011-11-16 at 09:09 -0800, Chris Kaynor wrote:
> On Wed, Nov 16, 2011 at 8:57 AM, Frederic Rentsch > <[hidden email]> 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 -- http://mail.python.org/mailman/listinfo/python-list |
|
Am 16.11.2011 19:39, schrieb Frederic Rentsch:
>> 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! How about using the excellent logging framework instead of rolling your own stuff? It can print the traceback, too. >>> import logging >>> logging.basicConfig() >>> log = logging.getLogger("mymodule") >>> try: ... raise ValueError("test") ... except Exception: ... log.exception("some message") ... ERROR:mymodule:some message Traceback (most recent call last): File "<stdin>", line 2, in <module> ValueError: test Christian -- http://mail.python.org/mailman/listinfo/python-list |
|
In reply to this post by Chris Kaynor
On 16/11/2011 17:09, Chris Kaynor wrote:
> On Wed, Nov 16, 2011 at 8:57 AM, Frederic Rentsch > <[hidden email]> 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). > 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. -- http://mail.python.org/mailman/listinfo/python-list |
|
In reply to this post by Frederic Rentsch-2
On 11/16/2011 11:57 AM, Frederic Rentsch wrote:
> 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. Bare except is a holdover from when exceptions could be strings rather than an instance of a subclass of BaseException. A Python 3 interpreter in effect runs code within a try-except block something like this: try: <your code> except BaseException as __exception__: <print traceback and exit> However, use Exception instead of BaseException in your code unless you REALLY know what you are doing and why. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list |
|
In reply to this post by Frederic Rentsch-2
On Wed, 16 Nov 2011 17:57:27 +0100, Frederic Rentsch wrote:
> 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" catches any exception that inherits from its argument, and all MySQL exceptions inherit from MySQLError, so something like this will catch only MySQL exceptions and nothing else: try: (command) except MySQLdb.MySQLError as e: print(e) Dan -- http://mail.python.org/mailman/listinfo/python-list |
| Powered by Nabble | Edit this page |
