Bare Excepts

Victor Subervi victorsubervi at gmail.com
Wed Dec 30 08:50:55 EST 2009


On Wed, Dec 30, 2009 at 9:23 AM, Jean-Michel Pichavant <
jeanmichel at sequans.com> wrote:

> 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.
>

Or just throw the exception once and grab it. Okay, thanks ;)
beno
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091230/cbace13c/attachment-0001.html>


More information about the Python-list mailing list