Why exceptions shouldn't be used for flow control [Re: YAS to the 
Author Message
 Why exceptions shouldn't be used for flow control [Re: YAS to the


>> while not f.eof():
>>   line = f.readline()
>>   # ...
>I'd love to be able to write reading loops that way.
>Unfortunately, it's a fact of life that some of the
>things read() has to deal with are unable to detect
>eof without trying to read something. I think the
>existing definition of read() and friends is the best
>that can be done in those circumstances.

I'd guess that you're talking about things like text file parsing and
such.  I would use readline there, but that's fine.  Yes, unstructured
data is more difficult to handle.

>> It's been a while since I've used read, so I
>> don't recall what it actually returns
>It returns an empty string if it can't read more
>than 0 characters, same as readline().

Ah.  There's a justification for that, as you observed.  I don't like the
fact that readline is stuck doing the same thing (record delimiters are
not really part of the record).

>> in a function designed to get n!=0
>> more characters, an EOF really is an error.  Unfortunately for me, that's
>> not the definition of 'read', and never will be.
>Perhaps there should be a 2-parameter version of read:
>   read(min, max)
>which would raise an exception if it couldn't read at
>least min characters. Setting min=0 would give the
>current behaviour, and setting min=max would allow
>reading a fixed-length record without having to check
>for errors. Then everyone would be happy!

A facinating solution.  Well, since I don't use read, I probably wouldn't
use it, and I'm the only person whining.  Therefore, it's probably not
worth implementing (even though it is a cool solution).


-William "Billy" Tanksley

Sun, 16 Dec 2001 03:00:00 GMT  
 [ 1 post ] 

 Relevant Pages 

1. Why you shouldn't use -activebackground

2. instead of C++ shouldn't ms have used masm for win95

3. enabling hardware flow control on a Sun's serial interface

4. Why doesn't IO#popen raise exceptions?

5. Control cmp used with 'in'?

6. Exception flow analysis in Ada

7. why doesn't my boolean controls work?

8. Double alignment probs: Why shouldn't I used -dalign always on sun4?

9. Shouldn't there be an Exit Block Statement in Smalltalk

10. Help! Deleted files I shouldn't have

11. Shouldn't I do so? [copyright law]

12. Shouldn't this be constant space?


Powered by phpBB® Forum Software