Reputation: 5144
From Java API
public class EOFException extends IOException
Signals that an end of file or end of stream has been reached unexpectedly during input.
This exception is mainly used by data input streams to signal end of stream. Note that many other input operations return a special value on end of stream rather than throwing an exception.
So why are data input streams so different from other input operations? Why doesn't it return a special value just like other input operations to signal the end of the stream? Because I think exception should only be used on really exceptional cases.
Upvotes: 1
Views: 430
Reputation: 311055
An out-of-band return value is required to signal EOS. All the in-band values are used when returning primitives, so there are no out-of-band values available, so it has to be an exception.
Same applies to ObjectInput.readObject()
. null
is an in-band value, so it can't be used to signal EOS.
This is different from InputStream.read()
, which returns either -1 or a byte value -128..127. In this case -1 is out of band.
One assumes that readUTF()
throws EOFException
for symmetry with the other methods, although it could probably have returned null at EOS.
Upvotes: 2