Why do I get the "Unhandled exception type IOException"?

Roman picture Roman · Feb 21, 2010 · Viewed 178.4k times · Source

I have the following simple code:

import java.io.*;
class IO {
    public static void main(String[] args) {    
       BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));    
       String userInput;    
       while ((userInput = stdIn.readLine()) != null) {
          System.out.println(userInput);
       }
    }
}

And I get the following error message:

----------
1. ERROR in io.java (at line 10)
    while ((userInput = stdIn.readLine()) != null) {
                        ^^^^^^^^^^^^^^^^
Unhandled exception type IOException
----------
1 problem (1 error)roman@roman-laptop:~/work/java$ mcedit io.java 

Does anybody have any ideas why? I just tried to simplify the code given on the sum web site (here). Did I oversimplify?

Answer

Marcin picture Marcin · Feb 21, 2010

You should add "throws IOException" to your main method:

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

You can read a bit more about checked exceptions (which are specific to Java) in JLS.