Why do I get the “Unhandled exception type IOException”?












29















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?










share|improve this question



























    29















    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?










    share|improve this question

























      29












      29








      29


      7






      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?










      share|improve this question














      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?







      java stdin readline ioexception






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 21 '10 at 13:14









      RomanRoman

      27.6k124277366




      27.6k124277366
























          5 Answers
          5






          active

          oldest

          votes


















          46














          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.






          share|improve this answer

































            39














            Java has a feature called "checked exceptions". That means that there are certain kinds of exceptions, namely those that subclass Exception but not RuntimeException, such that if a method may throw them, it must list them in its throws declaration, say: void readData() throws IOException. IOException is one of those.



            Thus, when you are calling a method that lists IOException in its throws declaration, you must either list it in your own throws declaration or catch it.



            The rationale for the presence of checked exceptions is that for some kinds of exceptions, you must not ignore the fact that they may happen, because their happening is quite a regular situation, not a program error. So, the compiler helps you not to forget about the possibility of such an exception being raised and requires you to handle it in some way.



            However, not all checked exception classes in Java standard library fit under this rationale, but that's a totally different topic.






            share|improve this answer































              11














              Try again with this code snippet:



              import java.io.*;

              class IO {
              public static void main(String args) {
              try {
              BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
              String userInput;
              while ((userInput = stdIn.readLine()) != null) {
              System.out.println(userInput);
              }
              } catch(IOException ie) {
              ie.printStackTrace();
              }
              }
              }


              Using try-catch-finally is better than using throws. Finding errors and debugging are easier when you use try-catch-finally.






              share|improve this answer

































                0














                Reading input from keyboard is analogous to downloading files from the internet, the java io system opens connections with the source of data to be read using InputStream or Reader, you have to handle a situation where the connection can break by using IOExceptions



                If you want to know exactly what it means to work with InputStreams and BufferedReader this video shows it






                share|improve this answer

































                  0














                  add "throws IOException" to your method like this:



                  public static void main(String args) throws  IOException{

                  FileReader reader=new FileReader("db.properties");

                  Properties p=new Properties();
                  p.load(reader);


                  }





                  share|improve this answer























                    Your Answer






                    StackExchange.ifUsing("editor", function () {
                    StackExchange.using("externalEditor", function () {
                    StackExchange.using("snippets", function () {
                    StackExchange.snippets.init();
                    });
                    });
                    }, "code-snippets");

                    StackExchange.ready(function() {
                    var channelOptions = {
                    tags: "".split(" "),
                    id: "1"
                    };
                    initTagRenderer("".split(" "), "".split(" "), channelOptions);

                    StackExchange.using("externalEditor", function() {
                    // Have to fire editor after snippets, if snippets enabled
                    if (StackExchange.settings.snippets.snippetsEnabled) {
                    StackExchange.using("snippets", function() {
                    createEditor();
                    });
                    }
                    else {
                    createEditor();
                    }
                    });

                    function createEditor() {
                    StackExchange.prepareEditor({
                    heartbeatType: 'answer',
                    autoActivateHeartbeat: false,
                    convertImagesToLinks: true,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: 10,
                    bindNavPrevention: true,
                    postfix: "",
                    imageUploader: {
                    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                    allowUrls: true
                    },
                    onDemand: true,
                    discardSelector: ".discard-answer"
                    ,immediatelyShowMarkdownHelp:true
                    });


                    }
                    });














                    draft saved

                    draft discarded


















                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2305966%2fwhy-do-i-get-the-unhandled-exception-type-ioexception%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    5 Answers
                    5






                    active

                    oldest

                    votes








                    5 Answers
                    5






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    46














                    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.






                    share|improve this answer






























                      46














                      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.






                      share|improve this answer




























                        46












                        46








                        46







                        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.






                        share|improve this answer















                        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.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited May 29 '17 at 4:02









                        Muhd

                        10.7k175369




                        10.7k175369










                        answered Feb 21 '10 at 13:18









                        MarcinMarcin

                        5,85653848




                        5,85653848

























                            39














                            Java has a feature called "checked exceptions". That means that there are certain kinds of exceptions, namely those that subclass Exception but not RuntimeException, such that if a method may throw them, it must list them in its throws declaration, say: void readData() throws IOException. IOException is one of those.



                            Thus, when you are calling a method that lists IOException in its throws declaration, you must either list it in your own throws declaration or catch it.



                            The rationale for the presence of checked exceptions is that for some kinds of exceptions, you must not ignore the fact that they may happen, because their happening is quite a regular situation, not a program error. So, the compiler helps you not to forget about the possibility of such an exception being raised and requires you to handle it in some way.



                            However, not all checked exception classes in Java standard library fit under this rationale, but that's a totally different topic.






                            share|improve this answer




























                              39














                              Java has a feature called "checked exceptions". That means that there are certain kinds of exceptions, namely those that subclass Exception but not RuntimeException, such that if a method may throw them, it must list them in its throws declaration, say: void readData() throws IOException. IOException is one of those.



                              Thus, when you are calling a method that lists IOException in its throws declaration, you must either list it in your own throws declaration or catch it.



                              The rationale for the presence of checked exceptions is that for some kinds of exceptions, you must not ignore the fact that they may happen, because their happening is quite a regular situation, not a program error. So, the compiler helps you not to forget about the possibility of such an exception being raised and requires you to handle it in some way.



                              However, not all checked exception classes in Java standard library fit under this rationale, but that's a totally different topic.






                              share|improve this answer


























                                39












                                39








                                39







                                Java has a feature called "checked exceptions". That means that there are certain kinds of exceptions, namely those that subclass Exception but not RuntimeException, such that if a method may throw them, it must list them in its throws declaration, say: void readData() throws IOException. IOException is one of those.



                                Thus, when you are calling a method that lists IOException in its throws declaration, you must either list it in your own throws declaration or catch it.



                                The rationale for the presence of checked exceptions is that for some kinds of exceptions, you must not ignore the fact that they may happen, because their happening is quite a regular situation, not a program error. So, the compiler helps you not to forget about the possibility of such an exception being raised and requires you to handle it in some way.



                                However, not all checked exception classes in Java standard library fit under this rationale, but that's a totally different topic.






                                share|improve this answer













                                Java has a feature called "checked exceptions". That means that there are certain kinds of exceptions, namely those that subclass Exception but not RuntimeException, such that if a method may throw them, it must list them in its throws declaration, say: void readData() throws IOException. IOException is one of those.



                                Thus, when you are calling a method that lists IOException in its throws declaration, you must either list it in your own throws declaration or catch it.



                                The rationale for the presence of checked exceptions is that for some kinds of exceptions, you must not ignore the fact that they may happen, because their happening is quite a regular situation, not a program error. So, the compiler helps you not to forget about the possibility of such an exception being raised and requires you to handle it in some way.



                                However, not all checked exception classes in Java standard library fit under this rationale, but that's a totally different topic.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Feb 21 '10 at 13:23









                                jkffjkff

                                12.9k23370




                                12.9k23370























                                    11














                                    Try again with this code snippet:



                                    import java.io.*;

                                    class IO {
                                    public static void main(String args) {
                                    try {
                                    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
                                    String userInput;
                                    while ((userInput = stdIn.readLine()) != null) {
                                    System.out.println(userInput);
                                    }
                                    } catch(IOException ie) {
                                    ie.printStackTrace();
                                    }
                                    }
                                    }


                                    Using try-catch-finally is better than using throws. Finding errors and debugging are easier when you use try-catch-finally.






                                    share|improve this answer






























                                      11














                                      Try again with this code snippet:



                                      import java.io.*;

                                      class IO {
                                      public static void main(String args) {
                                      try {
                                      BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
                                      String userInput;
                                      while ((userInput = stdIn.readLine()) != null) {
                                      System.out.println(userInput);
                                      }
                                      } catch(IOException ie) {
                                      ie.printStackTrace();
                                      }
                                      }
                                      }


                                      Using try-catch-finally is better than using throws. Finding errors and debugging are easier when you use try-catch-finally.






                                      share|improve this answer




























                                        11












                                        11








                                        11







                                        Try again with this code snippet:



                                        import java.io.*;

                                        class IO {
                                        public static void main(String args) {
                                        try {
                                        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
                                        String userInput;
                                        while ((userInput = stdIn.readLine()) != null) {
                                        System.out.println(userInput);
                                        }
                                        } catch(IOException ie) {
                                        ie.printStackTrace();
                                        }
                                        }
                                        }


                                        Using try-catch-finally is better than using throws. Finding errors and debugging are easier when you use try-catch-finally.






                                        share|improve this answer















                                        Try again with this code snippet:



                                        import java.io.*;

                                        class IO {
                                        public static void main(String args) {
                                        try {
                                        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
                                        String userInput;
                                        while ((userInput = stdIn.readLine()) != null) {
                                        System.out.println(userInput);
                                        }
                                        } catch(IOException ie) {
                                        ie.printStackTrace();
                                        }
                                        }
                                        }


                                        Using try-catch-finally is better than using throws. Finding errors and debugging are easier when you use try-catch-finally.







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Oct 23 '15 at 14:17









                                        Younes Regaieg

                                        3,66221334




                                        3,66221334










                                        answered Feb 21 '10 at 15:01









                                        ParthParth

                                        3711617




                                        3711617























                                            0














                                            Reading input from keyboard is analogous to downloading files from the internet, the java io system opens connections with the source of data to be read using InputStream or Reader, you have to handle a situation where the connection can break by using IOExceptions



                                            If you want to know exactly what it means to work with InputStreams and BufferedReader this video shows it






                                            share|improve this answer






























                                              0














                                              Reading input from keyboard is analogous to downloading files from the internet, the java io system opens connections with the source of data to be read using InputStream or Reader, you have to handle a situation where the connection can break by using IOExceptions



                                              If you want to know exactly what it means to work with InputStreams and BufferedReader this video shows it






                                              share|improve this answer




























                                                0












                                                0








                                                0







                                                Reading input from keyboard is analogous to downloading files from the internet, the java io system opens connections with the source of data to be read using InputStream or Reader, you have to handle a situation where the connection can break by using IOExceptions



                                                If you want to know exactly what it means to work with InputStreams and BufferedReader this video shows it






                                                share|improve this answer















                                                Reading input from keyboard is analogous to downloading files from the internet, the java io system opens connections with the source of data to be read using InputStream or Reader, you have to handle a situation where the connection can break by using IOExceptions



                                                If you want to know exactly what it means to work with InputStreams and BufferedReader this video shows it







                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Jan 21 '13 at 7:44









                                                Veger

                                                29.4k890104




                                                29.4k890104










                                                answered Jan 21 '13 at 1:27









                                                vivzvivz

                                                171




                                                171























                                                    0














                                                    add "throws IOException" to your method like this:



                                                    public static void main(String args) throws  IOException{

                                                    FileReader reader=new FileReader("db.properties");

                                                    Properties p=new Properties();
                                                    p.load(reader);


                                                    }





                                                    share|improve this answer




























                                                      0














                                                      add "throws IOException" to your method like this:



                                                      public static void main(String args) throws  IOException{

                                                      FileReader reader=new FileReader("db.properties");

                                                      Properties p=new Properties();
                                                      p.load(reader);


                                                      }





                                                      share|improve this answer


























                                                        0












                                                        0








                                                        0







                                                        add "throws IOException" to your method like this:



                                                        public static void main(String args) throws  IOException{

                                                        FileReader reader=new FileReader("db.properties");

                                                        Properties p=new Properties();
                                                        p.load(reader);


                                                        }





                                                        share|improve this answer













                                                        add "throws IOException" to your method like this:



                                                        public static void main(String args) throws  IOException{

                                                        FileReader reader=new FileReader("db.properties");

                                                        Properties p=new Properties();
                                                        p.load(reader);


                                                        }






                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Feb 7 '18 at 15:24









                                                        harun ugurharun ugur

                                                        39846




                                                        39846






























                                                            draft saved

                                                            draft discarded




















































                                                            Thanks for contributing an answer to Stack Overflow!


                                                            • Please be sure to answer the question. Provide details and share your research!

                                                            But avoid



                                                            • Asking for help, clarification, or responding to other answers.

                                                            • Making statements based on opinion; back them up with references or personal experience.


                                                            To learn more, see our tips on writing great answers.




                                                            draft saved


                                                            draft discarded














                                                            StackExchange.ready(
                                                            function () {
                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2305966%2fwhy-do-i-get-the-unhandled-exception-type-ioexception%23new-answer', 'question_page');
                                                            }
                                                            );

                                                            Post as a guest















                                                            Required, but never shown





















































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown

































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown







                                                            Popular posts from this blog

                                                            404 Error Contact Form 7 ajax form submitting

                                                            How to know if a Active Directory user can login interactively

                                                            TypeError: fit_transform() missing 1 required positional argument: 'X'