java 何时抛出 FileNotFoundException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34486268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:56:37  来源:igfitidea点击:

when to throw FileNotFoundException

java

提问by Gropai

I called a method named readFile()in the mainmethod, readFile()throws FileNotFoundException but maindoesn't, like the following:

我调用了一个在方法中命名readFile()main方法,readFile()抛出 FileNotFoundException 但main没有,如下所示:

public static void main(String[] args){
    readFile();
}

public static void readFile() throws FileNotFoundException{
    Scanner input = new Scanner(new File("file.txt"));
    ...
}

When I compiled the program, I got an error at readFile()in the mainmethod. It seems that I need to throw an exception in the header of mainas well. Why do I need to throw exception in both headers of mainand readFile()?

当我编译程序时,我readFile()main方法中遇到错误。似乎我也需要在标题中抛出异常main。我为什么要抛出的异常的两个头mainreadFile()

采纳答案by erip

Your options for handling Exceptionsare to catch them and deal with them immediately or to throw them in the function and propagate the exception up to the caller of the function.

您的处理选项Exceptions是捕获它们并立即处理它们,或者将它们扔到函数中并将异常传播给函数的调用者。

For main, it makes sense to catchand handle the exception there.

对于maincatch在那里处理异常是有意义的。

public static void main(String[] args){
  try {
    readFile();
  } catch (FileNotFoundException e) {
    // Do something with `e`
  }
}

public static void readFile() throws FileNotFoundException {
  Scanner input = new Scanner(new File("file.txt"));
  // ...
}

However, you could also do something like this:

但是,您也可以执行以下操作:

public static void main(String[] args){
  readFile();
}

public static void readFile() {
  try {
    Scanner input = new Scanner(new File("file.txt"));
    // ...
  } catch (FileNotFoundException e) {
    // Do something with `e` or handle it accordingly.
  }
}

I would advise against throwing exceptions in the main, but after that it's really a matter of whether you have a "back up" for if something fails. For more information, this questionhas fantastic detail.

我建议不要在 中抛出异常main,但在那之后,如果出现问题,您是否有“备份”真的很重要。有关更多信息,这个问题有非常详细的信息。

回答by Daniel Williams

Java has some controversy about its exceptions. It has two classes of exceptions. Checked and unchecked. Any exception extending from RuntimeException or Error is unchecked and does not need to be caught or explicitly declared as throwable in a method signature.

Java对其异常有一些争议。它有两类异常。选中和未选中。从 RuntimeException 或 Error 扩展的任何异常都是未经检查的,不需要在方法签名中被捕获或显式声明为可抛出的。

FileNotFound is however a checked exception and must either be caught or declared as throwable in the method signature.

然而 FileNotFound 是一个已检查的异常,必须在方法签名中被捕获或声明为可抛出。

The basic idea is that checked exceptions are ones that you may be able to recover from while unchecked exceptions are ones coming from a most likely unrecoverable error in programming.

基本思想是检查异常是您可以从中恢复的异常,而未检查异常是来自编程中最有可能无法恢复的错误的异常。

You can read all about it here: https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html

您可以在此处阅读所有相关信息:https: //docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html

回答by Drew Kennedy

Think of the throwskeyword as a promise; You're saying you're not going to catch the exception now, but you're going to catch it at the calling statement.

throws关键字想象成一个承诺;你是说你现在不会捕捉异常,但你会在调用语句中捕捉它。

The code you currently have in your readFilemethod seems perfectly valid, but you need to wrap the calling statement in a try-catchto appropriately handle the exception.

您当前在readFile方法中的代码似乎完全有效,但您需要将调用语句包装在 a 中try-catch以适当地处理异常。

回答by Nir Levy

there is no point to throw an exception in main method- this is the entry point to the program, which means no other method will catch this exception and handle it. you should catch and handle the exception here - either log the error, give a clear message to the user, read some other file, whatever, but throwing exception is wrong here.

在 main 方法中抛出异常是没有意义的——这是程序的入口点,这意味着没有其他方法会捕获这个异常并处理它。您应该在这里捕获并处理异常 - 要么记录错误,向用户提供明确的消息,要么读取其他文件,无论如何,但在这里抛出异常是错误的。

回答by myselfmiqdad

You need to catch the exception

你需要捕捉异常

public static void main(String[] args){
    readFile();
}

public static void readFile() {
   try {
      Scanner input = new Scanner(new File("file.txt"));
      ...
   } catch (FileNotFoundException ex) {
      // Error message
   } catch (Exception ex) {
      // Incase a different exception is caught
   }
}