try catch finall 结构里的 return
2024-08-29 09:03:21
public class ExceptionDemo1 { public static void main(String[] args) {
try {
int b = ;
int res = /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:)
catch...
finally...
over
相信这段代码,问题不大
throw和return:都可以结束方法
public class ExceptionDemo3 { public static void main(String[] args) {
try {
int b = ;
int res = /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:)
如果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=,b=; try {
res = /b;
} catch (Exception e) {
e.printStackTrace();
}finally{
res = ;
System.out.println("finally...");
}
return res; } }
java.lang.ArithmeticException: / by zero
finally...
at test.exception.ExceptionDemo4.test(ExceptionDemo4.java:)
at test.exception.ExceptionDemo4.main(ExceptionDemo4.java:)
这里没什么好说的
public class ExceptionDemo6 {
public static void main(String[] args) { int i =test();
System.out.println(i); } public static int test(){
int res=,b=; try {
res = /b;
} catch (Exception e) {
System.out.println("catch");
throw new RuntimeException(e.getMessage());
}finally{
res = ;
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:)
at test.exception.ExceptionDemo6.main(ExceptionDemo6.java:)
finally中的内容不论程序有无异常,都会被执行(除非在执行到finally之前jvm退出了),那么如果我们的程序在try和catch块中return了,finally中的还会执行吗?
故意把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 = ; try { System.out.println(a / ); a = ; } catch (ArithmeticException e) { a = ; return a; /* * return a在程序执行到这一步的时候, 这里不是return a而是return 30;这个返回路径就形成了。 * 但是呢,它发现后面还有finally,所以继续执行finally的内容,a=40 再次回到以前的返回路径,继续走return 30; */ } finally { a = ; } return a;
}
}
可以尝试在finally中return a;
观察结果:返回40
因为新的返回路径(return 40)生成,覆盖原来的return 30
最新文章
- centos下在线安装mysql
- Hadoop日记Day18---MapReduce排序分组
- 修改LR自带的示例程序端口号
- [Js]布局转换
- P - The Shortest Path in Nya Graph-hdu4725(双端队列+拆点)
- Java 泛型数组
- mac下Android apk 破解流程
- iOS开发中怎么样使用激光推送
- python自动化运维学习第一天--day1
- Hbase问题
- Jmeter学习系列----3 配置元件之计数器
- docker 搭建maven 私服
- 使用HashSet<;>;去除重复元素的集合
- 你应该学会的接口调试神器——Postman高级用法
- 手动更新nexus的索引
- 关于Spring的一点东西
- tar 命令显示进度条
- HTML 入门1
- PIN 引脚类型
- 一个数据仓库时代开始--Hive