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

alan.gauld@bt.com alan.gauld@bt.com
Sun, 19 Aug 2001 17:07:50 +0100


> >> > > for line in file:
> >> > >     try:
> >> > >         data, garbage = string.split(line, ':')
> >> > >     except ValueError:
> >               data = line
> 
> the more proper approach in my opinion would be to test if 
> there is a colon to begin with.  

The snag with that is that you potentially test every character 
in the string twice: once to see if a colon exists and again to 
split the line.

The approach above ensures that you only read it once. Potentially 
doubling performance if there is little post split processing!

Alan G