在Java中,如何强制异常"冒泡"?

Ale*_*lls 6 java exception

我有一个抛出异常的方法,它调用抛出异常的方法等等.所以"抛出异常"的几个方法是菊花链式的.调用submethod的第一个方法是将该子方法放在try-catch块中,该块捕获Exception在该调用中抛出的任何内容.理论上.实际上,try-catch块没有捕获到Exception.有没有办法解决这个问题?

这是代码:

          try {
                CSVSingleton.tryToReadBothFiles(FILE1_PATH, FILE2_PATH);

                } catch (Exception e) { // THIS BLOCK NEVER GETS ENTERED BY THE PATH O EXECUTION
System.out.println("There was an exception reading from at least one of the files. Exiting.");
                    System.exit(0);
                }
Run Code Online (Sandbox Code Playgroud)

这是CSVSingleton类的方法:

public static void tryToReadBothFiles(String filePath1, String filePath2) throws Exception {

        file1 = new CSVFileForDwellTime1(filePath1);
        file2 = new CSVFileForDwellTime2(filePath2);
    }
Run Code Online (Sandbox Code Playgroud)

这是来自CSVFileForDwellTime1类的代码:

public CSVFileForDwellTime1(String filePath) throws Exception {
        super(filePath);
    }
Run Code Online (Sandbox Code Playgroud)

然后这里是实际抛出原始FileNotFoundException的代码:

public GenericCSVFile(String filePath) throws Exception{
        this.filePath = filePath;
        try {
            fileReader = new FileReader(filePath);
            csvReader = new CSVReader(
                    fileReader);
            header = getActualHeaderNames();
        } catch (FileNotFoundException e) {
            System.out.println("Could not read file with name: " + filePath);
            // e.printStackTrace();
        }
    }
Run Code Online (Sandbox Code Playgroud)

我的猜测是,最后一个方法中的FileNotFoundException被catch块捕获,因此不会"冒泡".但有没有办法迫使它冒泡?

Ros*_*lle 15

立即回答:

你的想法是完全正确的,

    try {
        fileReader = new FileReader(filePath);
        csvReader = new CSVReader(
                fileReader);
        header = getActualHeaderNames();
    } catch (FileNotFoundException e) {
        System.out.println("Could not read file with name: " + filePath);
        // e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

这会抑制异常删除try-catch块(除非您可以实际执行异常操作)或在catch块中重新抛出它.

说明

通常有这样的checked例外,你有2个选择

  1. 捕获异常并执行一些操作来修复异常
  2. 将异常抛给调用者

你在这里做了什么属于第一类,除了你没有在catch块中做任何有用的事情(在这种情况下打印到控制台很少有用,因为异常消息本身通常有足够的信息来查看出错的地方)

第二类是通过不使用try-catch块并因此添加throws FileNotFoundException到方法签名来实现的.或者明确地throw说明您使用的异常:

catch(FileNotFoundException e)
{
    //do something
    throw e;
}
Run Code Online (Sandbox Code Playgroud)

然而,在这种情况下,如果do something不值得你不必要地抓住了一些东西只是为了把它扔掉.

你可以这样想:

Alice throws a ball to Charlie
Bob intercepts the ball
Bob then looks at the ball and then throws it to Charlie
Run Code Online (Sandbox Code Playgroud)

奖励积分

当您知道可能发生的异常时,请确保实际catchthrow该异常,而不是该异常的父级.以下面的方法签名为例:

public String method1() throws Exception

public String method2() throws FileNotFoundException
Run Code Online (Sandbox Code Playgroud)

这里method2清楚地告诉调用者可能发生什么,然后可以帮助找出调用异常的原因(无需读取代码或遇到错误).

其次可能发生其他异常,并且您可能会捕获错误的异常,请参考以下示例:

try{
    fileReader = new FileReader(filePath); //could potentially throw FileNotFoundException
    fileReader = null; //woops
    csvReader = new CSVReader(fileReader); //throws NullPointerException but the compiler will not know this 
    //....other stuff....//
}
catch(Exception e){
    // the compiler told me that a FileNotFoundException can occur so i assume that is the reason the catch has executed
    System.err.println("You have entered an invalid filename");
    //doing anything here that would fix a FileNotFoundException is pointless because that is not the exception that occured
}
Run Code Online (Sandbox Code Playgroud)