Results 1 to 6 of 6
  1. #1
    gisler is offline Member
    Join Date
    Apr 2009
    Posts
    26
    Rep Power
    0

    Default Calling on class with throws IOException

    Hi,

    I have created a form and when the user clicks a button i would like it to call on a method in my connection class.

    private void connectActionPerformed(ActionEvent evt) {
    Connect connection = new Connect();

    connection.startConnection(); //gives error -
    unreported exception java.io.lang.IOException; must be caught or declared to be thrown
    }

    my connection class method is:

    public void startConnection() throws IOException {

    BufferedReader inFromUser = new BufferedReader(newInputStreamReader(System.in)) ;
    //omited//

    i think it has something to do with the throws IOException at the end of my startconnection method.

    any help would be appreciated. many thanks
    gisler

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

    Default

    What happens if you follow the compiler's recommendation of placing the method call in a try/catch block?

    If this doesn't make sense, please have a look here: Exceptions Tutorial

    Much luck!

  3. #3
    gisler is offline Member
    Join Date
    Apr 2009
    Posts
    26
    Rep Power
    0

    Default

    Cool thanks. that fixed it. It now looks like this:

    private void connectActionPerformed(ActionEvent evt) {
    try{
    Connect connection = new Connect();
    connection.startConnection();
    }
    catch (IOException e){

    }
    }

    I do not understand why this is needed in this code since i only calling upon the method. what would there be here for me to catch?

    thanks

    gisler

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

    Default

    Your code is no good as your catch block is empty, meaning if an exception occurs (can't start the connection, etc...) you're just ignoring this information and throwing it away. Please read the tutorial that I've linked to above as it will explain all. Also, please use code tags when posting code (see my signature for details).

    Much luck.

  5. #5
    gisler is offline Member
    Join Date
    Apr 2009
    Posts
    26
    Rep Power
    0

    Default

    ok, i will do that. thanks for the help

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

Similar Threads

  1. change throws IOException
    By manhtungtnk28@gmail.com in forum New To Java
    Replies: 7
    Last Post: 11-23-2009, 08:39 AM
  2. Recursive Class throws NullPointerException
    By freeBatjko in forum New To Java
    Replies: 5
    Last Post: 11-03-2009, 09:18 AM
  3. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  4. problem calling function from class to class
    By alin_ms in forum New To Java
    Replies: 3
    Last Post: 12-19-2008, 07:35 PM
  5. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 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
  •