java.io.IOExecption; must be caugh or declared to be thrown - omg?
Hello everybody,
I'm kind of new to java and as part of a school project I've got to make a fully functional program.
When compiling (I use BlueJ, if it matters), I'm getting this message:
java.io.IOExecption; must be caugh or declared to be thrown
What does this mean?
I'm missing something(obviously), the IOExection, I think I saw that a mate from class has some part of code which does something when the wrong datatype or input is inputed, te IOExecption thingy.
Please help, I need to do this very quickly, and time is running out!
[584 byte] By [
Emira] at [2007-11-26 21:11:51]

How do I solve the problem?
Here goes the code:
public static int method (int methood){
String inputstring;
int input;
BufferedReader stdin = new BufferedReader(new InputStreamReader( System.in ));
inputstring = stdin.readLine(); // --> I get the error here.
input = java.lang.Integer.parseInt(inputstring);
return pass;}
-
btw thanks for your help! :)
Emira at 2007-7-10 2:49:11 >

you have two options:
1-:
import java.io.IOException;
public static int method (int methood) throws IOException{//you should import IOException first
String inputstring;
int input;
BufferedReader stdin = new BufferedReader(new InputStreamReader( System.in ));
inputstring = stdin.readLine(); // --> I get the error here.
input = java.lang.Integer.parseInt(inputstring);
return pass;
}
2:
public static int method (int methood) {
String inputstring;
int input;
try{
BufferedReader stdin = new BufferedReader(new InputStreamReader( System.in ));
inputstring = stdin.readLine(); // --> I get the error here.
input = java.lang.Integer.parseInt(inputstring);
return pass;
}catch(IOException e){
//e.printStacktrace();
}finally{//optional
}
}
hth
I'm still confused
How does the IOExecption work ?
before defining the main class, I've imported java.io.IOException
I've added to the method throws IOExeption
But I've still got the problem of the complier telling me that there is a unreported expection , must be caught or declared to be thrown
how do I, within the method declare the expection and what happens when wrong info is entered?
Emira at 2007-7-10 2:49:12 >

sorry, I forgot one important thing. When you use the first option (throw IOException), the method(s) that calls your static method should handle the IOException. See the tutorial http://java.sun.com/docs/books/tutorial/essential/exceptions/index.htmlhth
IOException is a "checked exception". That means that it's an exception that you must handle.
Exceptions are classes that represent something going wrong in the program. They're not likely to go wrong (that's why they're called "exceptions" and not "typicals"), but the developer of the API you're using felt that it was likely enough, and that it was something that you could reasonably deal with if it does go wrong. The exception represents that unlikely event.
Your job is to write some code that handles the exception. One way to handle it is just to declare your method as throwing that exception, and let the caller handle it. Another way to handle it is just to tell the user that it happened. In other cases, you can do more subtle handling -- say, you might be able to fix the core problem that the exception represents.
Read about try/catch blocks in the tutorials.
When you try to read a file, some exception may thrown. Say, the file can not be found, can not be read, etc.To prevent some unexpected things may happen, you have to catch this exception and do something. Say, tell the user to change the filename and read the file again.