【问题标题】:Why do I get the "Unhandled exception type IOException"?为什么我会得到“未处理的异常类型 IOException”?
【发布时间】:2011-01-19 08:13:54
【问题描述】:

我有以下简单的代码:

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);
       }
    }
}

我收到以下错误消息:

----------
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 

有人知道为什么吗?我只是试图简化 sum 网站 (here) 上给出的代码。我是否过于简单化了?

【问题讨论】:

    标签: java stdin readline ioexception


    【解决方案1】:

    即使我发现了异常,我也得到了错误。

        try {
            bitmap = BitmapFactory.decodeStream(getAssets().open("kitten.jpg"));
        } catch (IOException e) {
            Log.e("blabla", "Error", e);
            finish();
        }
    

    问题是没有导入 IOException

    import java.io.IOException;
    

    【讨论】:

      【解决方案2】:

      将“抛出 IOException”添加到您的方法中,如下所示:

      public static void main(String args[]) throws  IOException{
      
              FileReader reader=new FileReader("db.properties");
      
              Properties p=new Properties();
              p.load(reader);
      
      
          }
      

      【讨论】:

        【解决方案3】:

        您应该在主方法中添加“抛出 IOException”:

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

        您可以在JLS 中阅读更多关于检查异常(Java 特有)的信息。

        【讨论】:

          【解决方案4】:

          用这段代码 sn-p 再试一次:

          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();
                  }   
              }
          }
          

          使用try-catch-finally 比使用throws 更好。使用try-catch-finally 更容易查找错误和调试。

          【讨论】:

            【解决方案5】:

            从键盘读取输入类似于从 Internet 下载文件,java io 系统使用 InputStream 或 Reader 打开与要读取的数据源的连接,您必须处理使用 IOExceptions 可能中断连接的情况

            如果您想确切地知道使用 InputStreams 和 BufferedReader 意味着什么,this video 显示它

            【讨论】:

              【解决方案6】:

              Java 有一个称为“检查异常”的特性。这意味着存在某些类型的异常,即那些是 Exception 子类但不是 RuntimeException 的异常,因此如果一个方法可能抛出它们,它必须在其 throws 声明中列出它们,例如:void readData()抛出 IOException。 IOException 就是其中之一。

              因此,当您调用在其 throws 声明中列出 IOException 的方法时,您必须在自己的 throws 声明中列出它或捕获它。

              存在已检查异常的基本原理是,对于某些类型的异常,您绝不能忽视它们可能发生的事实,因为它们的发生是很正常的情况,而不是程序错误。因此,编译器可以帮助您不要忘记引发此类异常的可能性,并要求您以某种方式处理它。

              但是,并非 Java 标准库中的所有已检查异常类都符合这一基本原理,但这是一个完全不同的主题。

              【讨论】:

                猜你喜欢
                • 2016-07-04
                • 2021-11-01
                • 1970-01-01
                • 2023-03-12
                • 1970-01-01
                • 2015-12-20
                • 1970-01-01
                • 1970-01-01
                • 2020-08-16
                相关资源
                最近更新 更多