try-catch-finally执行顺序验证

try-catch-finally执行顺序验证(左边是.java文件,右边是.class文件)

提示:

try、catch块内的return操作编译后会变成把return的值保存到变量var的操作。

总结:

try、catch块内的return操作编译后会变成把return的值保存到变量var的操作.
(1)如果finally中有return则执行finally中的return;
(2)如果finally中无return则执行return var,finally块中的操作不会影响var的值(如果var的值是对象的内存地址,则finally块的操作可以改变该对象的属性值)。

验证

1.基本类型
1.1return在try-catch-finally块外部
在这里插入图片描述

执行结果如下:
1=============7
2=============8
3=============9
4=============9

1.2return在try、catch、finally块内部
在这里插入图片描述

执行结果如下
1=============7
2=============8
3=============9
4=============9

1.3return在try、catch块内部
在这里插入图片描述

执行结果如下
1=============7
2=============8
3=============9
4=============8

1.4return在try、finally块内部
在这里插入图片描述

执行结果如下
1=============7
2=============8
3=============9
4=============9

1.5return在catch,finally块内部
在这里插入图片描述

执行结果如下
1=============7
2=============8
3=============9
4=============9

1.6return在finally块内部
在这里插入图片描述

执行结果如下
1=============7
2=============8
3=============9
4=============9

2.String类型
2.1return在try-catch-finally块外部
在这里插入图片描述

执行结果如下
1=============6_try---------52815410
2=============6_try_catch---------693521006
3=============6_try_catch_finally---------1250421938
4=============6_try_catch_finally---------1250421938

2.2return在try、catch、finally块内部
在这里插入图片描述执行结果如下

1=============6_try---------52815410
2=============6_try_catch---------693521006
3=============6_try_catch_finally---------1250421938
4=============6_try_catch_finally---------1250421938

2.3return在try、catch块内部
在这里插入图片描述执行结果如下

1=============6_try---------52815410
2=============6_try_catch---------693521006
3=============6_try_catch_finally---------1250421938
4=============6_try_catch---------1250421938

2.4return在try、finally块内部
在这里插入图片描述执行结果如下

1=============6_try---------52815410
2=============6_try_catch---------693521006
3=============6_try_catch_finally---------1250421938
4=============6_try_catch_finally---------1250421938

2.5return在catch,finally块内部
在这里插入图片描述执行结果如下

1=============6_try---------52815410
2=============6_try_catch---------693521006
3=============6_try_catch_finally---------1250421938
4=============6_try_catch_finally---------1250421938

2.6return在finally块内部
在这里插入图片描述

执行结果如下

1=============6_try---------52815410
2=============6_try_catch---------693521006
3=============6_try_catch_finally---------1250421938
4=============6_try_catch_finally---------1250421938

3.User类型
3.1return在try-catch-finally块外部
在这里插入图片描述执行结果如下

1=============try_19---------1826771953
2=============try_catch_20---------1826771953
3=============try_catch_finally_21---------1826771953
4=============try_catch_finally_21---------1826771953

3.2return在try、catch、finally块内部
在这里插入图片描述执行结果如下

1=============try_19---------1826771953
2=============try_catch_20---------1826771953
3=============try_catch_finally_21---------1826771953
4=============try_catch_finally_21---------1826771953

3.3return在try、catch块内部
3.3.1
在这里插入图片描述

执行结果如下
1=============try_19---------1826771953
2=============try_catch_20---------1826771953
3=============try_catch_finally_21---------1826771953
4=============try_catch_finally_21---------1826771953

3.3.2
在这里插入图片描述

执行结果如下
1=============try_19---------1826771953
2=============try_catch_20---------1826771953
3=============try_catch_finally_21---------1406718218
4=============try_catch_20---------1406718218

3.4return在try、finally块内部
在这里插入图片描述执行结果如下

1=============try_19---------1826771953
2=============try_catch_20---------1826771953
3=============try_catch_finally_21---------1826771953
4=============try_catch_finally_21---------1826771953

3.5return在catch,finally块内部
在这里插入图片描述执行结果如下

1=============try_19---------1826771953
2=============try_catch_20---------1826771953
3=============try_catch_finally_21---------1826771953
4=============try_catch_finally_21---------1826771953

3.6return在finally块内部
在这里插入图片描述执行结果如下

1=============try_19---------1826771953
2=============try_catch_20---------1826771953
3=============try_catch_finally_21---------1826771953
4=============try_catch_finally_21---------1826771953

在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值