EMZETT.
Login

EOFException (EOF)

In short: EOF stands for “End Of File”. EOFException is thrown when reading unexpectedly reaches the end of a file or stream although more data was expected.

In more detail: It belongs to the IOExceptions and is checked. It is typical when reading binary data with DataInputStream or ObjectInputStream. For text files read() returns -1 instead and readLine() returns null.

In detail

  • Causes: truncated file, format read wrongly, loop reads too often, connection closed early.
  • Remedy: check the length before reading or test for -1/null. With streams, treat EOFException in a try/catch as the normal end.
  • Scanner: NoSuchElementException when no input is left, see try/catch/finally.
  • Other languages: Python EOFError (for input() without input), C: the EOF constant for getchar().

See also: Exception handling, throw and throws