Bare Excepts

Jean-Michel Pichavant jeanmichel at sequans.com
Wed Dec 30 08:23:01 EST 2009


Victor Subervi wrote:
> On Wed, Dec 30, 2009 at 9:09 AM, Victor Subervi 
> <victorsubervi at gmail.com <mailto:victorsubervi at gmail.com>> wrote:
>
>
>     Anyway, you should definitely use a coding rule checker, like
>     pylint or pyckeck. It would sometimes point you into the correct
>     direction. For instance, pylint will tell you that except: pass is
>     often (not always) a clue for bad design/pattern and issue
>     warnings for it.
>
> How about this:
>
>     try:
>       id = form.getfirst('id')
>       sql = 'update %s set ProdID="%s" Quantity="%s" %s where 
> ID="%s";' % (tmpTable, prodid, quantity, sqlUpdateMiddle, id)
>       cursor.execute(sql)
>       db.commit()
>     except:
>       raise
>  
> How does one get around sql calls that fail?
> beno
>
Rule N° 1:
put the minimum code in try blocks

Rule N°2:
dont use BARE EXCEPT, or you'll piss off MRAB for good :o). Beside from 
kidding, don't use bare except.

   id = form.getfirst('id')
   sql = 'update %s set ProdID="%s" Quantity="%s" %s where ID="%s";' % 
(tmpTable, prodid, quantity, sqlUpdateMiddle, id)
   try:
      cursor.execute(sql)
      db.commit()
    except NAME_OF_THE_EXCEPTION :
      raise # reraising without doing anything else is meaninless, you 
could remove the try block


In your example you must lookt at your sql module documentation to know 
which kind of exception it can throw.

JM



More information about the Python-list mailing list