JAVA基础之--try..catch..finally

先看一段代码:

public class Test {
    
    
    public static int method(){
        int a=0;
        try {
             int x = 1/0;
             a++;
        } catch (Exception e) {
            e.printStackTrace();
            a++;
            return a;
        }finally{
            a++;
            return a;
        }
    }
    public static void main(String[] args) {
        System.out.println(Test.method());
    }
}

 

输出结果:

java.lang.ArithmeticException: / by zero
    at test_try_catch.Test.method(Test.java:9)
    at test_try_catch.Test.main(Test.java:21)
2

 代码中,catch里的return 被finally里的return覆盖了。

posted on 2016-05-05 15:48  Junqiang  阅读(160)  评论(0编辑  收藏  举报

导航