[Tutor] anecdote in the workplace about try,except clause

Sean 'Shaleh' Perry shalehperry@home.com
Thu, 16 Aug 2001 19:32:44 -0700 (PDT)


So, thought i would pass this on.

A coworker who is a long time perl'er is learning python and asking me
questions now and then.  Today he was writing a file parser and did the
following:

for line in file:
    try:
        data, garbage = string.split(line, ':')
    except ValueError:
        pass

    print 'Data: %s' % data


His rationale was that some lines lack the ':' so he would just let the
exception hide the 'annoying' python exception assuming that data would be
assigned the whole line in that case.

What he failed to realize is that when the except clause is triggered items in
the try clause go out of scope.  So data would not exist.

I of course helped him to write this in a more sane way.