Java:try-catch错误,必须被捕获才能被抛出

时间:2015-11-09 10:03:08

标签: java try-catch

我试图创建一个加载文件的方法,但它没有按照应有的方式工作。为什么我会收到此错误?我的try-catch块有问题吗?

NamnMetod.java:157: error: unreported exception InterruptedException; must be caught or declared to be thrown
   EventQueue.invokeAndWait(new Runnable() {

这是我的代码:

   public static void hämtaFrånText() {
   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,"Filen hittades inte!");
            }
            catch(IOException e2) {     
                JOptionPane.showMessageDialog(null,"Det misslyckades");
            }
      }              
   });
}   

4 个答案:

答案 0 :(得分:0)

它与run()方法中的try / catch块无关。问题在于调用invokeAndWait ... EventQueue.invokeAndWait()的方法被声明为抛出InterruptedException,这是一个经过检查的异常......所以你需要另一个 try / catch块(在调用周围)或你的hämtaFrånText方法应该声明它也可以抛出InterruptedException

答案 1 :(得分:0)

根据JavaDoc(强调我自己):

  

public static void invokeAndWait(Runnable runnable)                             抛出 InterruptedException ,                                    的的InvocationTargetException

invokeAndWait可以抛出两种类型的异常。在您的方法中,您没有try-catch段来处理这些错误,因此您的方法必须指定它本身可能会抛出这些异常,因为它们不在内部处理。

你需要:

  1. throws InterruptedException添加到您的方法签名或
  2. 设置一个try-catch块,其中包含EventQueue.invokeAndWait(new Runnable() {...,以便处理任何例外情况。

答案 2 :(得分:0)

定义匿名类:

new Runnable() {
  @Override public void run() { ... }
};

基本上是定义本地类的简写:

class MyAnonymousRunnable implements Runnable {
  @Override public void run() { ... }
}

然后创建该类的实例:

new MyAnonymousRunnable();

因此,您的代码可以写成:

EventQueue.invokeAndWait(new MyAnonymousRunnable());

如果您有MyAnonymousRunnable的合适定义。 如果您这样做,您将在该行上获得完全相同的编译错误。但是,您知道如何在没有匿名类的代码中捕获异常:

try {
  EventQueue.invokeAndWait(new MyAnonymousRunnable());
} catch (InterruptedException e) { 
  Thread.currentThread().interrrupt();
  // Do whatever to handle the exception.
}

因此,如果您匿名定义该类,则没有什么区别:

try {
  EventQueue.invokeAndWait(new Runnable() {
    @Override public void run() { ... }
  });
} catch (InterruptedException e) { 
  Thread.currentThread().interrrupt();
  // Do whatever to handle the exception.
}

答案 3 :(得分:-1)

您可以将整个EventQueue.invokeAndWait(new Runnable(){...});代码封装在另一个try-catch块中,如下所示:

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, "Filen hittades inte!");
                } catch(IOException e2) {
                    JOptionPane.showMessageDialog(null, "Det misslyckades");
                }
            }
        });
    } catch(InterruptedException e3) {
        // your catch code here
    }
}