Confused compare function :)

Hans Mulder hansmu at xs4all.nl
Thu Dec 6 08:32:46 EST 2012


On 6/12/12 12:55:16, peter wrote:
> Is perfectly right to use try catch for a flow control.
> Just think in something more complex like this.
> 
>    try:
>             self._conn = MySQLdb.connect(host=host,
>                 user=user,
>                 passwd=passwd,
>                 db=db)
>     except:
>             logging.info("Error de conexion con la base de datos")
>             inform(subject = 'Db down on app %s' % app, body=sbody)

This is an example of the sort of incorrect code you
should try to avoid.  An improved version is:

   self._conn = MySQLdb.connect(host=host,
                user=user,
                passwd=passwd,
                db=db)

By not catching the exception, you're allowing the
Python interpreter to report what the problem was,
for example "Keyboard interrupt" or "Access denied".

By report "DB down" when there is no reason to assume
that that is the problem, you're confusing the user.

> Or maybe something like this.
> 
> try:
>             cursor.execute(sqli, data)
>             self._conn.commit()
> except:
>             try:
>                 self._conn.rollback()
>                 cursor.execute(sqli, data)
>                 self._conn.commit()
>             except Exception, e:
>                 pass
>                 # print e
>                 # logging.info('ERROR en la insercion %s' % e)

This is another example of what not to do.  Even the
commented-out print statement loses information, viz.
the traceback.

If you leave out the try/except, then more accurate
information will be printed, and a programmer who needs
to fix the problem, can run the code under the debugger
and it will automatically stop at the point where the
uncaught exception is raised.  That's much easier than
having to set breakpoints at all the "except Exception:"
clauses in a typical chunk of hard-to-maintain code.

Context managers were invented to make it easier to do
this sort of thing correctly.  For example:

    with sqlite3.connect(dbpath) as connection:
        connection.cursor().execute(sqli, data)

If the flow reaches the end of the "with" command,
the connection object will self.commit() automatically.
If an exception is raised, the connection object will
self.rollback() automatically.  No try/except required.

This is shorter, and much easier to get right.

> This is pretty dumb, but is a valid example, on what you can
> do with try catch

It is an unfortunate fact of life that you can write code
that is hard to maintain.  The fact that you *can* do this,
does not mean that you should.


Hope this helps,

-- HansM




More information about the Python-list mailing list