Results 1 to 6 of 6
  1. #1
    doha786 is offline Member
    Join Date
    Mar 2009
    Posts
    48
    Rep Power
    0

    Arrow unreported IOException problem

    hi,

    i am trying to call one method from different file but getting this error..
    callMethod.java:31: unreported exception java.io.IOException; must be caught or declared to be thrown
    i already reported this exception in that method but cant understand where else need to report ?
    Java Code:
    public class callMethod extends JFrame implements ActionListener {
    JButton button;
    
    callMethod(){
    button=new JButton("show");  button.addActionListener(this);
    
    add(button); 
    }
    
    
    public static void main(String[] args) throws IOException  {
    		callMethod cm=new callMethod();
    		cm.setSize(200,100);
    		cm.setVisible(true);
    		}
    
    
    public void actionPerformed (ActionEvent e) {
    if (e.getSource()==button) {
    
    MyFile file=new MyFile();
    String[][] show=file.method("get", "20"); //error mentioned in this line
    ..
    ..
    }
    }
    the method of MyFile:
    Java Code:
    public String[][] method(String name, String value) throws IOException {
    ...
    ..
    i can run it from MyFile but cant call from different file...

    pls help me...
    what we are thinking, it might not be true

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Java Code:
    public void actionPerformed (ActionEvent e)
    cannot throw an IOException. Even if you add is as


    Java Code:
    public void actionPerformed (ActionEvent e)  throws IOException
    it will complain (although differently).

    do this:

    Java Code:
    public void actionPerformed (ActionEvent e)  {
      try {
        // execute IOException-throwing stuff
      } catch (IOException ex) {
        // do something with it: ex.printStackTrace(), or throw a runtime exception, or simply ignore it.
      }
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by doha786 View Post
    Java Code:
    public void actionPerformed (ActionEvent e) {
       if (e.getSource()==button) {
    
          MyFile file=new MyFile();
          String[][] show=file.method("get", "20"); //error mentioned in this line
          ..
          ..
       }
    }

    the method, "method" throws the exception but your actionPerformed method calls this method and doesn't do anything with the exception. I recommend you do what the error is in essence telling you to do: put the method call in the actionPerformed in a try/catch block.

    Java Code:
    public void actionPerformed (ActionEvent e) {
       if (e.getSource()==button) {
    
          MyFile file=new MyFile();
          try {
             String[][] show=file.method("get", "20");
          }
          catch (IOException ioe) {
             ioe.printStackTrace();  // or whatever else may need to be done if file not found
          }
          
          ..
          ..
       }
    }

  4. #4
    doha786 is offline Member
    Join Date
    Mar 2009
    Posts
    48
    Rep Power
    0

    Default

    great great thanks........


    really appreciate you....

    Why i cant add reputation for you Fubarable??
    its showing message "you must spread some reputation around................"
    what we are thinking, it might not be true

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  6. #6
    doha786 is offline Member
    Join Date
    Mar 2009
    Posts
    48
    Rep Power
    0

    Default

    oh..ok

    anyway thanks a lot.. you made me smile :)
    what we are thinking, it might not be true

Similar Threads

  1. Unreported exception java.sql.SQLException
    By javamula in forum AWT / Swing
    Replies: 4
    Last Post: 09-29-2009, 02:32 PM
  2. Replies: 2
    Last Post: 09-18-2009, 11:22 PM
  3. IOException error
    By eeraj in forum AWT / Swing
    Replies: 3
    Last Post: 08-31-2009, 04:05 AM
  4. Error: unreported exception java.io.IOException; ??
    By jonsamwell in forum New To Java
    Replies: 5
    Last Post: 08-24-2008, 04:11 AM
  5. GUI IOException
    By serfster in forum New To Java
    Replies: 3
    Last Post: 06-13-2008, 04:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •