TryCatchFinally块

来源:互联网 发布:武汉知豆电动车租赁 编辑:程序博客网 时间:2024/05/17 04:55

程序代码如下:

public class Test {    public static void main(String[] args) {        System.out.println(getTestString());    }    public static void getException() throws Exception {        throw new Exception();    }    public static String getTestString() {        try {            getException();            return "return in try";        } catch (Exception e) {            return "return in catch";        } finally {            return "return in finally";        }    }}

程序打印结果:

return in finally

解析:

catch块也执行了,但是有finally块,finally块执行后,return语句被覆盖,所以结果为return in finally

如果去掉finally块,则结果为return in catch

0 0
原创粉丝点击