【问题标题】:Return statements in try/catch block Java在 try/catch 块 Java 中返回语句
【发布时间】:2015-04-19 17:09:06
【问题描述】:

假设我有以下内容:

class NegativeException extends RuntimeException {
}

class ZeroException extends NegativeException {
}

class Driver {
    static boolean Marathon(int a) {
        try {
            if (a < 0)
                throw new NegativeException();
            else if (a == 0)
                throw new ZeroException();
            else if (a >= 42)
                return true;
            else
                return false;
        } catch (ZeroException e) {
            System.out.println("Use natural number");
        } finally {
            System.out.println("One last thing");
        }
        System.out.println("All done.");
        return false;
    }

    public static void main(String[] args) {
        /* One last thing */
        /* true */
        System.out.println(Marathon(100));


        System.out.println(Marathon(0));
        System.out.println(Marathon(-5));

    }
}

我想了解的是,为什么在使用我们的 main 方法的第一行时没有执行“All done”行? Marathon(100)

似乎finally 语句执行,然后输出return 语句。我知道finally 块将始终执行,无论发生什么。但是,我似乎无法理解 return 语句如何影响 try catch 块的流。尝试从try-cath-finally 块返回时,是否有一套适用的规则?

【问题讨论】:

  • "class ZeroException extends NegativeException" 嗯....记住,“扩展”意味着存在“是”关系。 “0 是负数”是不正确的。

标签: java try-catch


【解决方案1】:

我想了解的是,为什么在使用我们的 main 方法的第一行时没有执行“All done”行?马拉松(100)

因为a &gt;= 42 是真的,所以你这样做:

return true;

...立即将控制权转移到finally 块;在finally 块的末尾,函数返回(没有运行任何行跟随finally 块)。也就是说,return 不只是设置返回值,它会在运行任何未完成的 finally 块之后终止函数。

如果您想继续执行,您可以写入一个变量,然后在末尾有一个 return

static boolean Marathon(int a) {
    boolean rv = false;
    try {
        if (a < 0)
            throw new NegativeException();
        else if (a == 0)
            throw new ZeroException();
        else if (a >= 42)
            rv = true;
    } catch (ZeroException e) {
        System.out.println("Use natural number");
    } finally {
        System.out.println("One last thing");
    }
    System.out.println("All done.");
    return rv;
}

更多关于trycatch 内的return:如果您从具有finally 块的try 内发出return,它会立即将控制权转移到finally 块。当到达该块的末尾时,函数终止(finally 块之后运行任何代码,除非您嵌套了finally 块或类似的块)。如果您在catchreturn,也会发生同样的事情。

例如:

try {
    if (someCondition) {
        return 1;
    }
    if (someOtherCondition) {
        throw new Exception();
    }
}
catch (Exception e) {
    System.out.println("Got here because of exception");
    return 2;
}
finally {
    System.out.println("Got here");
}
System.out.println("May not have gotten here");
return 3;

"Got here"始终输出,无论如何;这就是 finally 子句的意义所在,它们总是会被执行。

"Got here because of exception" 只会在someOtherCondition 为真时输出(并且会在"Got here" 之前输出),在这种情况下函数会正常返回值1

如果someConditionsomeOtherCondition 为真,则不会输出"May not have gotten here",因为trycatch 块中的returns。

如果两个条件都不成立,您会看到"May not have gotten here" 后跟"Got here",并且函数返回3

注意catch 块中的return 表示当someOtherCondition 为真时,函数返回正常(值为2),它不会抛出。如果你没有 return 并且 someOtherCondition 是真的,你会看到 "Got here because of exception""Got here" 然后函数会以一个 throw 终止(根本没有返回值),并且会不输出"May not have gotten here"

最后但同样重要的是:如果您在 finally 块中有 return,则 return “获胜”:即使您在 finally 块中,因为您已经发布了 @987654370 @,finally 块中的return 取代它,使函数返回finally 所说的值,而不是之前的值。

【讨论】:

  • @JBNizet: Right: "...它在该点终止函数(除了finally 子句等)" 表示“除了finally 子句和这样的”——例如,他们被处决。我重新措辞一下,也许“禁止”并不是每个人都清楚。
  • @T.J.Crowder 太棒了!这只是澄清了一切!谢谢!
  • @Dimitri:很高兴有帮助。我把这些东西折叠成答案。 :-)
  • @Dimitri:啊!抱歉,已修复——看,你现在是专家了!
  • @T.J.Crowder 只是确保我覆盖了我所有的踪迹! :)
猜你喜欢
  • 2014-08-25
  • 2021-12-25
  • 2018-10-14
  • 2012-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-10
相关资源
最近更新 更多