繁体   English   中英

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

[英]Java: try-catch error, must be caught to be thrown

我试图创建一种加载文件的方法,但它没有按应有的方式工作。 为什么我会收到这个错误? 我的 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");
            }
      }              
   });
}   

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

根据JavaDoc (强调我自己的):

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

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

您需要:

  1. throws InterruptedException添加到您的方法签名或
  2. 有一个包含EventQueue.invokeAndWait(new Runnable() {...try-catch块,以便可以处理任何异常。

定义匿名类:

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

您可以封装整个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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM