Eof - to return or to throw?

Dawid Ciężarkiewicz dawid.ciezarkiewicz at asn.pl
Tue Feb 13 07:45:37 PST 2007


Kristian Kilpi wrote:

> Reaching EOF is not an error, of course, but reading while at EOF should
> be IMO.

I see that your talking about something more then just error - exception
thing which I dissagree (see in reply to Frank Benoit's post).

I think it's quite natural that if file/socket is in Eof but in it's buffer
there is still some unread data then read should first return that data and
then - where there is nothing to return and will not be because file/socket
connection has ended - Eof should be thrown.

In fact if we want the code of:
   return uint readWithThrowingEof(void[] buf);
   return int  readWithReturningEof(void[] buf);

behave the same then both readWithReturningEof and readWithThrowingEof can
not signalise Eof until all data were returned normaly.

So that all your code will work like it should. In fact your've just pointed
out additional advantage of throwing Eof instead of returning it. (...)

> For example, all the bytes are read from a file:
> 
>    File f;
>    while(f.isNotEnd())
>      buf ~= f.read();
>
> No exception handling is required because we don't make assumptions about
> the contents of the file.

(...) Like in your code - if someone don't want to use try { } catch he will
probably don't have to.

Thanks. :)



More information about the Digitalmars-d mailing list