简体   繁体   English

嵌套 Try-Catch 块未捕获异常

[英]Nested Try-Catch Block Not Catching Exception

My program is attempting to scan through my directory in search of the existence of.cmp or.txt files.我的程序正在尝试扫描我的目录以搜索是否存在 .cmp 或 .txt 文件。

If fileName were to equal "test" and if neither test.cmp nor test.txt files existed, my program would still throw a FileNotFoundException despite my try-catch block under the first catch.如果 fileName 等于“test”并且 test.cmp 和 test.txt 文件都不存在,我的程序仍然会抛出 FileNotFoundException,尽管我的 try-catch 块在第一个捕获下。 I've tried moving the second try-catch block around, but nothing seems to work – everything I test the code out with a file that doesn't exist still ends up throwing an exception.我尝试移动第二个 try-catch 块,但似乎没有任何效果——我用一个不存在的文件测试代码的所有内容最终都会引发异常。

public int checkFileExistence() {
        BufferedReader br = null;
        int whichFileExists = 0;


        try {//check to see if a .cmp exists
            br = new BufferedReader(new FileReader(fileName + ".cmp")); 
            whichFileExists = 0;// a .cmp exists
        }

        catch (IOException e){ //runs if a .cmp file has not been found
            try {//check to see if a .txt file exists
                br = new BufferedReader(new FileReader(fileName + ".txt"));
                whichFileExists = 1;//a .txt file exists
            }
            catch (IOException e2) {//if no .txt (and .cmp) file was found  

                e2.printStackTrace();
                whichFileExists = 2; //no file exists

            }

        }

        finally {   

            try {
                br.close();
            } 

            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


        return whichFileExists;
    }


I would expect the program to work, but each time I test the program, the program throws a FileNotFoundException where it says "test.txt" doesn't exist.我希望程序能够工作,但是每次我测试程序时,程序都会抛出一个 FileNotFoundException,它说“test.txt”不存在。

It is printing that exception because of this line:由于这一行,它正在打印该异常:

e2.printStackTrace();

It's working as you are expecting, just printing the error it got.它按您的预期工作,只是打印它得到的错误。 You can remove these printStackTrace() calls if you don't want to see them.如果您不想看到它们,可以删除这些printStackTrace()调用。 Well, don't remove the one in the last catch block, otherwise you would never know if there is a problem there.好吧,不要删除最后一个 catch 块中的那个,否则你永远不会知道那里是否有问题。

On a separate note, this design is totally based on exceptions, which is not recommended.另外需要说明的是,这种设计完全基于异常,不推荐。 I'm sure there are methods in the File class to check for existence of files.确定File class 中有一些方法可以检查文件是否存在。

This program is working as expected...该程序按预期工作...

catch (IOException e2) {//if no .txt (and .cmp) file was found  

    e2.printStackTrace();
    whichFileExists = 2; //no file exists

}

Above catch clause catches your IOException and prints it with e2.printStackTrace();上面的 catch 子句捕获您的 IOException 并使用e2.printStackTrace();打印它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM