【发布时间】:2018-06-29 13:09:05
【问题描述】:
我目前对为什么收到“FileNotFoundException 的无法访问的 catch 块”感到困惑。当我尝试运行我的代码时。我从主方法参数中获取文件路径,并在输入路径错误或在路径中找不到文件的实例中捕获错误。
有人可以帮我解决这个问题吗?这是我这部分的代码:
public void readFile(String inputFilePath, String outputFilePath) throws IOException{
StringBuilder sb = new StringBuilder();
File input = null;
try{
input = new File(inputFilePath);
}
catch (FileNotFoundException e){
System.err.println("Input file cannot be found in the provided path");
}
【问题讨论】:
-
构造函数
File(String)不会抛出FileNotFoundException。 -
文件对象只是对可能存在或不存在的文件或目录路径的引用,这就是
File类型具有exists()方法的原因。
标签: java exception-handling try-catch filenotfoundexception