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

相關文章
相關標籤/搜索