try-catch-finally return 的案例分析

返回值为3,说明catch代码执行完毕,又去执行了finally。 

    public static int funTest(){
        int a = 0;
        try {
            int b = 5/a;
            a = 1;
            return a;
        }catch (Exception e){
            a = 2;
            System.out.println("catch部分");
            return a;
        }finally {
            a = 3;
            System.out.println("finally部分");
            return a;
        }
    }

catch部分
finally部分
3

 

 

    public static int funTest(){
        int a = 0;
        try {
            int b = 5/a;
            a = 1;
            return a;
        }catch (Exception e){
            a = 2;
            System.out.println("catch部分");
            return a;
        }finally {
            a = 3;
            System.out.println("finally部分");
            //return a;
        }
    }

catch部分
finally部分
2

你可能感兴趣的:(Java)