java try catch结构_try catch finall 结构里的 return

public class ExceptionDemo1 {

public static void main(String[] args) {

try {

int b = 0;

int res = 10/b;

System.out.println(res);

} catch (Exception e) {

e.printStackTrace();

System.out.println("catch...");

}finally{

System.out.println("finally...");

}

System.out.println("over");

}

16 }

输出:

java.lang.ArithmeticException: / by zero

at test.exception.ExceptionDemo1.main(ExceptionDemo1.java:8)

catch...

finally...

over

相信这段代码,问题不大

throw和return:都可以结束方法

public class ExceptionDemo3 {

public static void main(String[] args) {

try {

int b = 0;

int res = 10/b;

System.out.println(res);

} catch (Exception e) {

System.out.println("catch...");

throw new RuntimeException(e.getMessage());

//System.out.println("catch...");

}finally{

System.out.println("finally...");

}

System.out.println("over");

}

}

结果:

catch...

finally...

Exception in thread "main" java.lang.RuntimeException: / by zero

at test.exception.ExceptionDemo3.main(ExceptionDemo3.java:12)

如果catch有throw语句,在catch块中throw后面的语句执行不到(报错)。

此时仍然会执行finally语句,但不会执行finally后面的语句

public class ExceptionDemo4 {

public static void main(String[] args) {

int i =test();

System.out.println(i);

}

public static int test(){

int res=0,b=0;

try {

res = 10/b;

} catch (Exception e) {

e.printStackTrace();

}finally{

res = 9;

System.out.println("finally...");

}

return res;

}

}

java.lang.ArithmeticException: / by zero

finally...

at test.exception.ExceptionDemo4.test(ExceptionDemo4.java:17)

at test.exception.ExceptionDemo4.main(ExceptionDemo4.java:8)

9

这里没什么好说的

public class ExceptionDemo6 {

public static void main(String[] args) {

int i =test();

System.out.println(i);

}

public static int test(){

int res=0,b=0;

try {

res = 10/b;

} catch (Exception e) {

System.out.println("catch");

throw new RuntimeException(e.getMessage());

}finally{

res = 9;

System.out.println("finally...");

}

return res;//不会执行

}

23 }

结果:

catch

finally...

Exception in thread "main" java.lang.RuntimeException: / by zero

at test.exception.ExceptionDemo6.test(ExceptionDemo6.java:18)

at test.exception.ExceptionDemo6.main(ExceptionDemo6.java:6)

finally中的内容不论程序有无异常,都会被执行(除非在执行到finally之前jvm退出了),那么如果我们的程序在try和catch块中return了,finally中的还会执行吗?

4e4f2939af4ceb04a804cdb9a4b1a2c0.png

故意把filename写错,造出异常,输出为下:

this is catch_for_filenot… block!

this is finally block!

this is main return value:false

从这儿看出来,程序先输出catch块中的,后又去执行finally块中的,虽然在catch中已经返回了,最后执行mian方法中的,而且输出false,说明catch块中的也成功返回了。

所以,面对疑问,我们可以很肯定的回答,即使有return语句,finally块也一定会被执行!

public class FinallyDemo2 {

public static void main(String[] args) {

System.out.println(getInt());

}

public static int getInt() {

int a = 10;

try {

System.out.println(a / 0);

a = 20;

} catch (ArithmeticException e) {

a = 30;

return a;

/*

* return a在程序执行到这一步的时候,

这里不是return a而是return 30;这个返回路径就形成了。

* 但是呢,它发现后面还有finally,所以继续执行finally的内容,a=40

再次回到以前的返回路径,继续走return 30;

*/

} finally {

a = 40;

}

return a;

}

}

可以尝试在finally中return a;

观察结果:返回40

因为新的返回路径(return 40)生成,覆盖原来的return 30

https://www.cnblogs.com/wihainan/category/894809.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值