Reputation: 37
I am getting the above error when calling a method that itself will throw an exception. I have used this same method of calling and throwing errors before and had no issues before. If someone could explain what is going on and give an example of how it should work it would be appreciated.
//Menu choice execution
if (intMenu == 1) {
loadArray();
}
else if (intMenu == 2){
export();
}
Both of those calls are stopping the compiler and giving the following error:
Error: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
What confuses me is that the methods themselves are throwing the exception on their instantiation.
//Load method that will export the arrays
public static export() throws FileNotFoundException {
}
//Load method that will search the arrays
public static search() throws FileNotFoundException {
}
Any help would be much appreciated.
Upvotes: 0
Views: 550
Reputation: 12795
throws FileNotFoundException
after a method declaration means that this method can potentially throw such an exception. It doesn't mean it immediately throws it (throws
is different from throw
, which is a keyword for actually throwing an exception).
Since Java knows that those methods can throw such an exception, it requires you to handle it -- to either make your own method also be declared with throws FileNotFoundException
or to catch it.
See this page for more details, especially the part The Catch or Specify Requirement
Upvotes: 2