<div class="gmail_quote">On Wed, Dec 30, 2009 at 9:04 AM, Jean-Michel Pichavant <span dir="ltr"><<a href="mailto:jeanmichel@sequans.com">jeanmichel@sequans.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div></div><div class="h5">Victor Subervi wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi;<br>
I'll trouble-shoot bare excepts as I work on new code. I'll trouble-shoot the others that don't (seem to) cause problems later. Here's a new one:<br>
<br>
    for optionsStore, storeOptions in ourOptions().iteritems():<br>
      if store == optionsStore:<br>
        for option in storeOptions:<br>
          try:<br>
            fromForm = form.getfirst(option)<br>
            try:<br>
              fromForm, junk = string.split(fromForm, ':')<br>
            except:<br>
              pass # This is only an expedient to split options that have a colon in them, such as the colors<br>
            fromForm = string.replace(fromForm, '-', '')<br>
            sql = 'select "%s" from %s%s t join %s p where p.ID=t.ID;' % (fromForm, store, (option[0].upper() + option[1:]), store)<br>
#            print sql<br>
            cursor.execute(sql)<br>
            try:<br>
              optionsPrices += cursor.fetchone()[0]<br>
            except TypeError:<br>
              pass # There is no options price for this.<br>
          except:<br>
            raise<br>
 If there are no values entered into table (in this case "productsSizes") for the given product ID, then "optionsPrices" should not be added unto. Am I doing this correctly? Or is there a better way than capturing ALL TypeErrors?<br>

TIA,<br>
beno<br>
</blockquote></div></div>
Hello,<br>
<br>
Do not use the 'try except' clause when 'if then' can perfectly handle the case.<br>
<br>
i.e.<div class="im"><br>
           try:<br>
             fromForm, junk = string.split(fromForm, ':')<br>
           except:<br>
             pass # This is only an expedient to split options that have a colon in them, such as the colors<br>
<br></div>
would be better written<div class="im"><br>
<br>
# This is only an expedient to split options that have a colon in them, such as the colors<br></div>
if ':' in fromForm:<br>
   fromForm, junk = fromForm.split(':')<br>
<br>
or<br>
<br>
fromForm = fromForm.split(':')[0]<br>
<br>
<br>
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.<br>

<br>
In a more general manner, use try blocks when the code can throw exceptions upon condition you do not control. When you know about the conditions, use the if statement.<br>
<br>
No:<br>
<br>
a = None<br>
try:<br>
   a.split()<br>
except AttributeError:<br>
   pass<br>
<br>
<br>
Yes:<br>
<br>
a = None<br>
if a:<br>
   a.split()<br>
<br>
Exception are required in the following example:<br>
<br>
try:<br>
   main()<br>
except KeyboardInterrupt:<br>
   print 'interrupted by user'<br>
<br>
Cheers,<br><font color="#888888">
<br>
JM<br>
<br>
</font></blockquote></div>Thank you.<br>beno<br>