Java: what are IOEXceptions in BufferedReader's readLine() for?

The basic idea is that a BufferedReader delegates to a different kind of Reader, so it is passing on that exception.

That different kind of Reader can read from some kind of volatile external resource, say a file system in the case of FileReader. A file system read can fail for many reasons at any time. (The situation is worse if the Reader is getting its underlying data from a Network Stream). The file could get deleted out from under you (depending on the file system and OS involved).

Because you cannot predict what will happen with code, you get a checked exception - the point being that the API is telling you that you should think about the fact that this operation may not work out even if there is nothing wrong with your code.


BufferedReader.readLine() is declared as potentially throwing an exception, see: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/BufferedReader.html#readLine()

You either need to catch it, or declare your main method as throwing IOException.

Ie, either do this:

try {
    while((s=in.readLine()) != null){
        System.out.println(s);
     }
} catch(IOException e) {
    // Code to handle the exception.
}

Or

public static void main(String[] args) throws IOException { ...

  1. It won't "continously ignite" them, it just might throw them each time you invoke it. In your case, if it throws something it means something has gone badly wrong with your standard input.
  2. The goal is to ensure that you, the programmer using the API, deals with the problem, since it is in general assumed to be a recoverable problem - although in your particular case it will be fatal for your whole program.