finally 中不能使用写return

 当try  一段代码后,要在在catch 中捕获后再进行抛出,但若是在finally中有return 语句,那么catch中的异常抛出将变得无效,以下面代码:java

static boolean testEx() {
        boolean ret = true;
        try {
            throw new RuntimeException("something error");
        } catch (Exception e) {
            throw e;
        } finally {
            return true;
        }
    }

    public static void main(String[] args) {
        System.out.println(testEx());
    }

结果: truecode

相关文章
相关标签/搜索