Reputation: 420
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);
and tells me
unreported exception java.io.ioexception must be caught or declared to be thrown
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).
The complete code is below:
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
}
Upvotes: 3
Views: 5731
Reputation: 200148
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.
Upvotes: 3
Reputation: 3412
Declare your PIFace before the try block and then surround the initialization of it with try{ }
, and put a catch block right after that to handle the exception. If you just surround it with a try block, without first initializing it, then the scope of the variable will be limited ONLY to that block. So do something like:
PiFace piface;
try{
piface = new PiFaceDevice(PiFace.DEFAULT_ADDRESS, Spi.CHANNEL_0);
}
{catch(Exception e) {
e.printStackTrace();
}
Upvotes: 0