3

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 } 
4
  • 3
    Did you really need to post all that code? And yes, it has to do with try-catch and throws. Commented Jan 14, 2014 at 15:59
  • With the try/catch uncommented, what error do you receive then? Commented Jan 14, 2014 at 16:01
  • I assume nearly all of it is superfluous to the question, but which parts, I'm not sure. Commented Jan 14, 2014 at 16:01
  • Ah yes, another victim of Java's checked exceptions. A language feature meant to help beginners actually forcing begginners into making errors. Commented Jan 14, 2014 at 16:09

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

This seems like the most comprehensive answer so far. However two new errors are introlduced: Cannot find symbol (this is at the catch IOExeption) symbol: class IOExeption location: class relay.Relay1 Cannot find symbol (this is at the RuntimeExeption) symbol: class RuntimeExeption location class relay.Relay1
That's an entirely unrelated problem. Let NetBeans solve it by adding the appropriate import statement.
OK thankyou, I'm using a lightweight IDE on the Pi to do small edits. I'll transfer it back over to my main machine to make the additional edits.
In that case, add import java.io.IOException;.
Oh wait I just realised I already have that import. Check the code above (before you edited it).
|
0

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(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.