SO I am reading a text file which has 2 columnn
foo, bar
SO i did something like
for each_line in f:
each_line = each_line.split(',')
foo = int(each_line[0])
bar = int(each_line[1]
foobar = FooBar(foo,bar)
foobar_list.append(foobar)
Now, sometimes either foo or bar is blank.. hence cant be typecasted to int..
Is there a way that if something where foo or bar is empty I can just skip this feature
(doesnt get appended to foodbar) but then the loops stills keeps on going ??
There are a couple of places this could possibly fail, either if each_line doesn’t have enough commas, or if either value was not a number (say, an empty string). In each case, you can catch the error and use
continueto skip to the next item inf:I’ve broken this down into two, separate exception handlers because there are two different ways for it to fail. You actually could fold it up like:
Since it’s the same exception and they happen close together. Personally, i prefer the former to make it really clear that there are two kinds of errors. What you should NOT do is:
because
FooBar()or evenfoobar_list.append()could fail, but the exception handler can swallow it; Always make thetry:suite in yoru exception handlers as small as possible so that they only catch one error, and where the error is is easy to find.