Results 1 to 5 of 5
  1. #1
    RKhadder is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default Catching and Throwing IOException Problem

    This is my code
    Java Code:
    private String[][] getPasswords(String file) throws IOException
        {
            
            try
            {
                BufferedReader in = new BufferedReader(new FileReader(file));
                String line;
                File f = new File(in.readLine());
                long fileSize = f.length();
                int length = (int)fileSize;
                String[][] correctPasswords= new String[length][2];
                    for(int i = 0; i < length; i+=2)
                    {
                        line = in.readLine();
                        correctPasswords[i][0] = line;
                        line = in.readLine();
                        correctPasswords[i][1] = line;
                    }
                return correctPasswords;
                in.close();
            }
            catch (IOException e)
            {
                JOptionPane.showMessageDialog(controllingFrame, "Invalid Employee Code", "Error Message", JOptionPane.ERROR_MESSAGE);
            }
        }
    I try to compile my code and I get an error code here:
    Java Code:
    private boolean isPasswordCorrect(char[] input)
        {
              String[][] correctPasswords = getPasswords(file);
              ...
         }
    The error code says
    unreported exceptio java.io.IOExceptionl; must be caught or declared to be thrown
    How do I fix this?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    The error message explains what you need to do: must be caught or declared to be thrown
    Either put the code in a try{}catch block
    Or declare the method the code is in to throw that exception
    Look at the getPasswords() method. It does both

  3. #3
    RKhadder is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default

    However when I do that since checkPassword is used by other methods I end up having to throw or catch exceptions eveywhere and then it starts looking messy. Is there anything else I can do?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You can only throw checked exceptions from a method if the method declares these exceptions. See what you've done in isPasswordCorrect.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default

    Does getPasswords() actually throw an IOException?
    I'm only asking because you catch one in that method and don't rethrow it.

Similar Threads

  1. Help with Catching Exceptions
    By javaman1 in forum New To Java
    Replies: 4
    Last Post: 09-11-2010, 02:06 AM
  2. After catching the exception thrown
    By scoobyrox in forum New To Java
    Replies: 2
    Last Post: 09-05-2010, 02:29 PM
  3. Catching exception from another library (class)
    By ezilka in forum New To Java
    Replies: 15
    Last Post: 05-26-2010, 06:59 AM
  4. unreported IOException problem
    By doha786 in forum New To Java
    Replies: 5
    Last Post: 04-20-2010, 11:14 PM
  5. Replies: 2
    Last Post: 09-18-2009, 11:22 PM

Posting Permissions

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