未报告的异常java.io.ioexception必须被捕获或声明为抛出 [英] unreported exception java.io.ioexception must be caught or declared to be thrown

查看:676
本文介绍了未报告的异常java.io.ioexception必须被捕获或声明为抛出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个编译错误是什么意思,如何解决?
编译器将错误指向第86行

What does this compile error mean and how can I resolve it? The compiler points the error at line 86

final PiFace piface = new PiFaceDevice(PiFace.DEFAULT_ADDRESS, Spi.CHANNEL_0);

并告诉我

unreported exception java.io.ioexception must be caught or declared to be thrown

它是与它需要一个try / catch的东西吗?这是我从搜索中找到的最好的答案,但我不知道如何实现它,我有一个去,它只是产生更多的错误(你可以看到它的注释掉)。

Is it something to do with it needing a try/catch? As that's the best answer I've found from my searches however I'm not really sure how to implement it i had a go at that and it just produced more errors (you can see it's commented out).

完整的代码如下:

public class Relay1 extends javax.swing.JFrame {

    public Relay1() {
        initComponents();
    }

    private void initComponents() {
        // stuff that doesn't matter...
    }


    //try{
        final PiFace piface = new PiFaceDevice(PiFace.DEFAULT_ADDRESS, Spi.CHANNEL_0);
    //}catch(IOException e){
        //System.out.println("Something went wrong...");
    //}


    public static void main(String args[]) throws InterruptedException, IOException {        
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Relay1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Relay1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Relay1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Relay1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Relay1().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    // End of variables declaration//GEN-END:variables
}


推荐答案

您确实没有任何处理代码为您的检查异常提供。如果该异常只传播给调用者,在这种情况下写入 new Relay1()的地方将是完全可以接受的。要实现这一点,写如下:

You really don't have any handling code to offer for your checked exception. It will be perfectly acceptable if that exception just propagates to the caller, in this case the place where new Relay1() is written. To achieve this, write as follows:

final PiFace piface; { 
  try {
    piface = new PiFaceDevice(PiFace.DEFAULT_ADDRESS, Spi.CHANNEL_0);
  } catch(IOException e) { 
    throw new RuntimeException("Failed to create Pi Face Device", e); 
  }
}

这将允许您保存诊断信息异常,并满足编译器的愿望。

This will allow you to both preserve the diagnostic information in the exception, and to satisfy the compiler's wish.

这篇关于未报告的异常java.io.ioexception必须被捕获或声明为抛出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆