finally简单学习


学而不思则罔,思而不学则殆

结论

  1. try中只要没有System.exit(0)类似的退出,finally就一定会执行
  2. 只要finally中有返回值,那么最终的返回值一定是finally中的
  3. 如果try中有返回值,这个是时候在,finally中修改,是不会修改其返回值的

测试一

    public static void main(String[] args) {
        TestMain testMain = new TestMain();
        int tyr3 = testMain.testTry();
        System.out.println("testTry return:" + tyr3);
    }

    private int testTry() {
        try {
            System.out.println("testTry try");
            System.exit(0);
            return 1;
        } catch (SecurityException e) {
            System.out.println("testTry SecurityException");
            e.printStackTrace();
            return 2;
        } catch (Exception e) {
            System.out.println("testTry Exception");
            e.printStackTrace();
            return 3;
        } finally {
            System.out.println("testTry finally");
            return 4;
        }
    }

结果:

testTry try

结果运行到System.exit(0),程序就终止,所以finally不一定会被执行

测试二

    public static void main(String[] args) {
        TestMain testMain = new TestMain();
        int tyr3 = testMain.testTry();
        System.out.println("testTry return:" + tyr3);
    }

    private int testTry() {
        try {
            System.out.println("testTry try");
            return 1;
        } catch (SecurityException e) {
            System.out.println("testTry SecurityException");
            e.printStackTrace();
            return 2;
        } catch (Exception e) {
            System.out.println("testTry Exception");
            e.printStackTrace();
            return 3;
        } finally {
            System.out.println("testTry finally");
            return 4;
        }
    }

结果:

testTry try
testTry finally
testTry return:4

在try和finally中都有return方法,的情况下,最终finally方法会执行,返回值也是finally块返回的值

测试三

   public static void main(String[] args) {
        TestMain testMain = new TestMain();
        int tyr3 = testMain.testTry();
        System.out.println("testTry return:" + tyr3);
    }

    private int testTry() {
        try {
            System.out.println("testTry try");
            if (true) {
                int x = 1 / 0;
            }
            return 1;
        } catch (SecurityException e) {
            System.out.println("testTry SecurityException");
            e.printStackTrace();
            return 2;
        } catch (Exception e) {
            System.out.println("testTry Exception");
            e.printStackTrace();
            return 3;
        } finally {
            System.out.println("testTry finally");
            return 4;
        }
    }

结果:

testTry try
testTry Exception
testTry finally
testTry return:4
java.lang.ArithmeticException: / by zero
	at TestMain.testTry(TestMain.java:18)
	at TestMain.main(TestMain.java:8)

测试在try中抛出异常,其中try,catch和finally块中的方法都执行了,但是返回值还是finally中的返回值

测试四

    public static void main(String[] args) {
        TestMain testMain = new TestMain();
        int tyr3 = testMain.testTry();
        System.out.println("testTry return:" + tyr3);
    }

    private int testTry() {
        try {
            System.out.println("testTry try");
            return 1;
        } catch (SecurityException e) {
            System.out.println("testTry SecurityException");
            e.printStackTrace();
            return 2;
        } catch (Exception e) {
            System.out.println("testTry Exception");
            e.printStackTrace();
            return 3;
        } finally {
            System.out.println("testTry finally");
        
        }
    }

结果:

testTry try
testTry finally
testTry return:1

finally 块中没有返回方法,try和finally都被执行了,最终的返回值是try中的

测试五

    public static void main(String[] args) {
        TestMain testMain = new TestMain();
        int tyr3 = testMain.testTry();
        System.out.println("testTry return:" + tyr3);
    }

    private int testTry() {
        try {
            System.out.println("testTry try");
            //System.exit(0);
            if (true) {
                int x = 1 / 0;
            }
            return 1;
        } catch (SecurityException e) {
            System.out.println("testTry SecurityException");
            e.printStackTrace();
            return 2;
        } catch (Exception e) {
            System.out.println("testTry Exception");
            e.printStackTrace();
            return 3;
        } finally {
            System.out.println("testTry finally");
        }
    }

结果

testTry try
testTry Exception
testTry finally
testTry return:3
java.lang.ArithmeticException: / by zero
	at TestMain.testTry(TestMain.java:17)
	at TestMain.main(TestMain.java:8)

finally中没有返回值方法,且try中抛出异常,try,catch和finally都执行,返回值是catch中的返回值

测试六

    public static void main(String[] args) {
        TestMain testMain = new TestMain();
        String tyr3 = testMain.test();
        System.out.println("test:" + tyr3);
    }

    public String test() {
        try {
            System.out.println("test try block");
            return test1();
        } finally {
            System.out.println("test finally block");
        }
    }

    public String test1() {
        System.out.println("test1 block");
        return "test1 return";
    }

结果:

test try block
test1 block
test finally block
test:test1 return

最终返回结果是test1方法的返回值,且注意其执行顺序

测试七

    public static void main(String[] args) {
        TestMain testMain = new TestMain();
        String tyr3 = testMain.test();
        System.out.println("test:" + tyr3);
    }

    public String test() {
        try {
            System.out.println("test try block");
            return test1();
        } finally {
            System.out.println("test finally block");
            return "test return";
        }
    }

    public String test1() {
        System.out.println("test1 block");
        return "test1 return";
    }

结果:

test try block
test1 block
test finally block
test:test return

最终返回结果是test的finally块中方法的返回值,且注意其执行顺序

测试返回值

测试1

    public static void main(String[] args) {
        TestMain testMain = new TestMain();
        int tyr3 = testMain.testTry();
        System.out.println("test:" + tyr3);
    }

    private int testTry() {
        try {
            return 0;
        } finally {
            return 1;
        }
    }

结果

test:1

测试2

    public static void main(String[] args) {
        TestMain testMain = new TestMain();
        int tyr3 = testMain.testTry();
        System.out.println("test:" + tyr3);
    }

    private int testTry() {
        int i = 0;
        try {
            i = 4;
            return i;
        } finally {
            System.out.println("finally:" + i);
            i++;
            System.out.println("finally:" + i);
        }
    }

结果:

finally:4
finally:5
test:4

测试3

    public static void main(String[] args) {
        TestMain testMain = new TestMain();
        int tyr3 = testMain.testTry();
        System.out.println("test:" + tyr3);
    }

    private int testTry() {
        int i = 0;
        try {
            i = 4;
            return i;
        } finally {
            System.out.println("finally:" + i);
            i++;
            System.out.println("finally:" + i);
            return i;
        }
    }

结果:

finally:4
finally:5
test:5
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值