FileNotFoundException的Java不可访问捕获块

FileNotFoundException的Java不可访问捕获块,java,exception-handling,try-catch,filenotfoundexception,Java,Exception Handling,Try Catch,Filenotfoundexception,我现在很困惑,当我试图运行我的代码时,为什么会收到“FileNotFoundException的不可访问捕获块”。我从主方法参数中获取文件路径,并捕获实例中的错误,即输入路径错误或在路径中找不到文件 谁能帮我一下吗?这是我的这部分代码: public void readFile(String inputFilePath, String outputFilePath) throws IOException{ StringBuilder sb = new StringBuilder();

我现在很困惑,当我试图运行我的代码时,为什么会收到“FileNotFoundException的不可访问捕获块”。我从主方法参数中获取文件路径,并捕获实例中的错误,即输入路径错误或在路径中找不到文件

谁能帮我一下吗?这是我的这部分代码:

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");
    }
因为这条线

input=新文件(inputFilePath)

不抛出FileNotFoundException

如果您深入研究
新文件(…)
的代码,这就是它所拥有的

public File(String pathname) {
     if (pathname == null) {
         throw new NullPointerException();
     }
     this.path = fs.normalize(pathname);
     this.prefixLength = fs.prefixLength(this.path);
}
正如您所看到的,此方法不会抛出
FileNotFoundException
,只有NPE的可能性

如果您要扩展代码以这样读取文件

new BufferedInputStream(new FileInputStream(input));  
那么
FileNotFoundException
就有意义了。试试看。

因为这行

input=新文件(inputFilePath)

不抛出FileNotFoundException

如果您深入研究
新文件(…)
的代码,这就是它所拥有的

public File(String pathname) {
     if (pathname == null) {
         throw new NullPointerException();
     }
     this.path = fs.normalize(pathname);
     this.prefixLength = fs.prefixLength(this.path);
}
正如您所看到的,此方法不会抛出
FileNotFoundException
,只有NPE的可能性

如果您要扩展代码以这样读取文件

new BufferedInputStream(new FileInputStream(input));  

那么
FileNotFoundException
就有意义了。试试看。

构造函数
文件(字符串)
不会抛出
FileNotFoundException
。文件对象只是对文件或目录路径的引用,这些路径可能存在,也可能不存在,这就是
文件
类型具有
exists()
方法的原因。构造函数
文件(字符串)
不会抛出
FileNotFoundException
。文件对象只是对文件或目录路径的引用,这些路径可能存在,也可能不存在,这就是为什么
File
类型有一个
exists()
方法。我很困惑,因为当我故意弄乱路径时,它给了我FileNotFoundException,当我调试时,它是在new File()创建中。据我所知,只有这一行
新文件(inputFilePath)
是不可能的。你能给我举个例子来重现这个问题吗?我想它实际上可能是文件声明行正下方的扫描仪。我查看了api,它看起来像扫描仪抛出FileNotFoundException。我刚刚了解了File exists()方法,所以我可能应该使用该方法。我很困惑,因为当我故意弄乱路径时,它给了我FileNotFoundException,而当我调试它时,它是在创建新文件()时。就我所知,只有这一行
新文件(inputFilePath)
是不可能的。你能给我举个例子来重现这个问题吗?我想它实际上可能是文件声明行正下方的扫描仪。我查看了api,它看起来像扫描仪抛出FileNotFoundException。我刚刚了解了File exists()方法,所以我可能应该改用它