Reading Until EOF

Michael Geary Mike at DeleteThis.Geary.com
Tue Oct 21 17:13:39 EDT 2003


> > Per my experience with C++, [end of file] does not qualify
> > as "exceptional behavior".  Frankly, it is [expected].  I'd
> > like to save the exceptions for true exceptions.

> But it *is* exceptional behavior for the file read to fail.  The
> normal behavior is for the call to succeed, right?

In C++, it would be considered bad style to use an exception to handle EOF.
The idea is that reaching EOF is something that you *expect* to happen, even
if only once. Similarly, the StopIteration exception would seem very odd to
a C++ programmer, since you expect to reach the end of an iteration
eventually.

You might use a C++ exception to handle something like a disk read
error--something that you do not expect to happen in the normal course of
events.

I realize the philosophy in Python is different--just trying to help clarify
why the OP is troubled by Python's more liberal use of exceptions. Anyone
coming from a C++ background is likely to have the same reaction at first.

-Mike






More information about the Python-list mailing list