Results 1 to 5 of 5
- 09-25-2010, 05:00 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 7
- Rep Power
- 0
Catching and Throwing IOException Problem
This is my code
I try to compile my code and I get an error code here: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); } }
The error code saysJava Code:private boolean isPasswordCorrect(char[] input) { String[][] correctPasswords = getPasswords(file); ... }
How do I fix this?unreported exceptio java.io.IOExceptionl; must be caught or declared to be thrown
- 09-25-2010, 05:10 PM #2
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
- 09-25-2010, 05:20 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 7
- Rep Power
- 0
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?
- 09-25-2010, 05:21 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You can only throw checked exceptions from a method if the method declares these exceptions. See what you've done in isPasswordCorrect.
- 09-27-2010, 01:22 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Help with Catching Exceptions
By javaman1 in forum New To JavaReplies: 4Last Post: 09-11-2010, 02:06 AM -
After catching the exception thrown
By scoobyrox in forum New To JavaReplies: 2Last Post: 09-05-2010, 02:29 PM -
Catching exception from another library (class)
By ezilka in forum New To JavaReplies: 15Last Post: 05-26-2010, 06:59 AM -
unreported IOException problem
By doha786 in forum New To JavaReplies: 5Last Post: 04-20-2010, 11:14 PM -
unreported exception IOException -- Yet I AM catching it?
By Agathorn in forum New To JavaReplies: 2Last Post: 09-18-2009, 11:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks