Java.IO.FileNotFoundException 调用方法时

Java.IO.FileNotFoundException when calling method

我在调用一个本身会抛出异常的方法时遇到上述错误。我以前使用过同样的调用和抛出错误的方法,之前没有遇到任何问题。如果有人可以解释发生了什么并举例说明它应该如何工作,我们将不胜感激。

//Menu choice execution
if (intMenu == 1) { 
  loadArray();
}
else if (intMenu == 2){
  export();
}

这两个调用都会停止编译器并给出以下错误:

Error: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown

让我感到困惑的是,方法本身在实例化时抛出异常。

  //Load method that will export the arrays
  public static export() throws FileNotFoundException {

  }

  //Load method that will search the arrays
  public static search() throws FileNotFoundException {

  }

如有任何帮助,我们将不胜感激。

throws FileNotFoundException 在方法声明之后意味着这个方法可能会抛出这样的异常。并不是说马上抛出(throwsthrow不同,后者是实际抛出异常的关键字)

因为 Java 知道那些方法会抛出这样的异常,它需要你处理它——要么让你自己的方法也用 throws FileNotFoundException 声明,要么捕获它。

this page for more details, especially the part The Catch or Specify Requirement