【问题标题】:Java BufferedReader error with try block带有 try 块的 Java BufferedReader 错误
【发布时间】:2015-11-09 08:30:38
【问题描述】:

我有这个方法应该加载一个文件,但它给了我这个错误:

NamnMetod.java:175: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
                 BufferedReader inFil = new BufferedReader(new FileReader(fil));
                                                           ^
NamnMetod.java:177: error: unreported exception IOException; must be    caught or declared to be thrown
                 String rad = inFil.readLine();  
                                            ^

这是我的代码:

   public static void hämtaFrånText() {
    try {
     EventQueue.invokeAndWait(new Runnable() {
     @Override

        public void run() {
              String aktuellMapp = System.getProperty("user.dir");
           JFileChooser fc = new JFileChooser(aktuellMapp);
           int resultat = fc.showOpenDialog(null);

              if (resultat != JFileChooser.APPROVE_OPTION) {
                 JOptionPane.showMessageDialog(null, "Ingen fil valdes!");
                 System.exit(0);
              }
                 String fil = fc.getSelectedFile().getAbsolutePath();
                 String[] namn = new String[3];         
                 String output ="";         
                 BufferedReader inFil = new BufferedReader(new FileReader(fil));                    
                 String rad = inFil.readLine();

                 int antal = 0;

                    while(rad != null){                  
                        namn[antal] = rad;
                        rad = inFil.readLine();
                        antal++;
                    }
                    inFil.close();

       }
    });                     
    }catch(IOException e2) {        
        JOptionPane.showMessageDialog(null,"The file was not found!");
    }           
}

我很困惑,因为我同时捕获了 IOException 和 FileNotFoundException 但我仍然收到此错误...

【问题讨论】:

  • 请遵循正确的命名约定。很难理解您要做什么。
  • 尝试将 try-catch 放入 public void run()-method。
  • @UmaKanth 我认为命名约定很好;它恰好是瑞典语。
  • 正如@KevinCruijssen 所说。您在hämtaFrånText() 方法中捕获IOException;在run() 方法中,抛出该异常的语句所在的位置没有try / catch
  • 我将 try-catch 放在 public void 中,但仍然出现错误:NamnMetod.java:157: error: unreported exception InterruptedException; must be caught or declared to be thrown EventQueue.invokeAndWait(new Runnable()

标签: java try-catch bufferedreader


【解决方案1】:

您在创建 Runnable 的代码中捕获它们,而需要在 Runnable.run() 中捕获异常。

run 方法中移动 try/catch。

另外,使用 try-with-resources 确保 FileReader 始终关闭,即使发生异常:

try (FileReader fr = new FileReader(fil);
     BufferedReader inFil = new BufferedReader(fr)) {
  // ...

  // No need to close `fr` or `inFil` explicitly.
}

【讨论】:

    【解决方案2】:

    try-catch 块属于“Runnable”的 run()-Method - 此 run()-Method 不得抛出任何异常。提交 Runnable 不会抛出异常。

    【讨论】:

      【解决方案3】:

      解决方法如下:

      public static void hämtaFrånText() {
      
              try {
                  EventQueue.invokeAndWait(new Runnable() {
      
                      @Override
                      public void run() {
                          try {
                              String aktuellMapp = System.getProperty("user.dir");
                              JFileChooser fc = new JFileChooser(aktuellMapp);
                              int resultat = fc.showOpenDialog(null);
      
                              if (resultat != JFileChooser.APPROVE_OPTION) {
                                  JOptionPane.showMessageDialog(null, "Ingen fil valdes!");
                                  System.exit(0);
                              }
                              String fil = fc.getSelectedFile().getAbsolutePath();
                              String[] namn = new String[3];
                              String output = "";
                              BufferedReader inFil = new BufferedReader(new FileReader(fil));
                              String rad = inFil.readLine();
      
                              int antal = 0;
      
                              while (rad != null) {
                                  namn[antal] = rad;
                                  rad = inFil.readLine();
                                  antal++;
                              }
                              inFil.close();
                          }catch(FileNotFoundException e1) {
                              JOptionPane.showMessageDialog(null,"The file was not found!");
                          }
                          catch(IOException e2) {
                              JOptionPane.showMessageDialog(null, "Failed!");
                          }
                      }
                  });
              } catch (InterruptedException e) {
                  e.printStackTrace();
              } catch (InvocationTargetException e) {
                  e.printStackTrace();
              }
      
          }
      

      Reason : run 是 Runnable 中的一个方法,在 eventQueue 中已经定义了它,并且在方法定义中没有处理异常的地方或在方法声明语句中的 throw 中添加异常。因此 FileNoFoundException 和 IOException 都应该在方法内部而不是外部给出。

      此外,EventQueue 抛出了 InterruptedException 检查异常,我添加了另一个 try catch 来处理该异常。

      希望对你有帮助。

      【讨论】:

      • 这个解决方案到底显示了什么?转储一大堆代码的答案不是很有帮助,除非它们实际上解释了您所做的更改。
      【解决方案4】:

      你需要在 run 方法中移动你的 try catch 块,这应该可以解决问题,你还需要处理 EventQueue.invokeAndWait(Runnable) 抛出的 InvocationTargetExceptionInterruptedException 以正确编译你的类。

      【讨论】:

        【解决方案5】:

        这里有范围问题。

        您使用的 try 和 catch 将捕获 hämtaFrånText() 的异常,但不会捕获方法 run() 的异常

        异常可能发生在方法run 内部而不是外部。

        你给出的 try-Cathc 只会关心在它的范围内发生的异常,因为你没有抛出这些异常,它不是你的 hämtaFrånText() 方法来处理 run() 内发生的事情。

        如果run 是您定义的方法,那么您可以抛出它们。 但现在唯一的选择就是在 run() 方法中捕获它们。

        那就试试吧

        public void run() {
        try{
        .............
        ...........
        
            }catch(FileNotFoundException e1) {
                JOptionPane.showMessageDialog(null,"The file was not found!");
                }
             catch(IOException e2) {
                JOptionPane.showMessageDialog(null, "Failed!");
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-04
          • 1970-01-01
          相关资源
          最近更新 更多