On Thu, Jan 8, 2009 at 2:21 PM, wesley chun <span dir="ltr"><<a href="mailto:wescpy@gmail.com">wescpy@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
except:<br>
pass<br>
<br>
try not to code these 2 lines in anything that you do because it will<br>
come back to haunt you when something is not working right but you<br>
can't find any errors. that's because this code masks and throws away<br>
everything!!<br>
<br>
if you are guaranteed that the strings all contain valid integers,<br>
then you don't have to worry about problems calling int().<br>
</blockquote><div><br>OP said we couldn't be sure that all rows would have the same number of elements; I'm afraid I extrapolated that we also might not be sure they were all integers (or, I should say, strings representing integers...), so there are two potential error types: IndexError and ValueError. I assumed that if either one occurred, we would want to leave that particular list member (if it exists) as-is (hence the pass).<br>
<br>I see three ways around this: <br>- test each list for length, and each member for integer-ness, before casting anything; <br>- catch the exception, test to see whether it's an IndexError or a ValueError, and if not then do something; <br>
- print/log every exception. <br><br>The first one violates "It's better to ask forgiveness than to ask permission" (and it's slow), the second one's a pain in the butt; the third one gets slow if LoL is large and has many non-standard members. Is there a fourth way?<br>
<br>Actually, I'm asking because I've run into similar situations and been dissatisfied with my own solution. Generally, if there are two or more "acceptable" errors that you can foresee, but you still want to catch any others, what's an elegant, readable, and computationally-cheap way to do it? Maybe a dictionary of exceptions...?<br>
</div></div><br>-- <br><a href="http://www.fsrtechnologies.com">www.fsrtechnologies.com</a><br>