try catch finall 结构里的 return
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
try catch finall 结构里的 return的更多相关文章
- Java中try catch finally语句中含return语句的执行情况总结-编程陷阱
前言:有java编程基础的人对java的异常处理机制都会有一定了解,而且可能感觉使用起来也比较简单,但如果在try catch finally语句块中遇到return语句,开发者可能就会遇到一些逻辑问 ...
- 【转】Java中try catch finally语句中含有return语句的执行情况(总结版)
Java中try catch finally语句中含有return语句的执行情况(总结版) 有一点可以肯定,finally块中的内容会先于try中的return语句执行,如果finall语句块中也有r ...
- 错误处理之try、catch、finally中的return、throw执行顺序。
今天遇到一个让人无语的代码块 try { bilSheetService.syncUser(bilWebseviceLog, userId, optType); }catch (Exception e ...
- Java笔试面试总结—try、catch、finally语句中有return 的各类情况
前言 之前在刷笔试题和面试的时候经常会遇到或者被问到 try-catch-finally 语法块的执行顺序等问题,今天就抽空整理了一下这个知识点,然后记录下来. 正文 本篇文章主要是通过举例的方式来阐 ...
- 今天在2cto网站看到一个有关try{}catch(){}finally{}语句中含有return的讲解,理解很透彻。
publicclassTrycatchTest{ publicstaticvoidmain(String[]args){ System.out.println("x:"+newTr ...
- 深拷贝和浅拷贝以及void里的return用法
Object o1=new Object(); Object o2; int i1=3,i2; 浅拷贝 o2=o1;i2=i1; 深拷贝 o2=new Object();o2=o1.clone(); ...
- try、catch、finally--try块里有return,finally还执行吗?
finally块的作用是,保证无论出现什么情况,finally块里的代码一定会被执行. 由于程序执行return就意味着结束对当前函数的调用并跳出这个函数体,所以任何语句要执行都只能在return之前 ...
- Java中try catch finally语句中含有return语句的执行情况(总结版)
在这里看到了try >但有一点是可以肯定的,finally块中的内容会先于try中的return语句执行,如果finall语句块中也有return语句的话,那么直接从finally中返回了,这也 ...
- Java中try catch finally语句中含有return语句的执行情况
finally块中的内容会先于try中的return语句执行,如果finall语句块中也有return语句的话,那么直接从finally中返回了,这也是不建议在finally中return的原因.下面 ...
随机推荐
- python面试题之如何计算一个字符串的长度
在我们想计算长度的字符串上调用函数len()即可 >>> len('hhhhhhhhjg') 10 所属网站分类: 面试经典 > python 作者:外星人入侵 链接:http ...
- SQL Server 2014与TFS 2013的错误(TF53001:管理员已取消数据库操作)
服务器环境: - TFS 2013 with Update 2 - SQL Server 2014(两个节点数据库服务器配置了AlwaysOn高可用性) - Windows Server 2012 R ...
- WPF制作歌词动画
最近再做一个UWP的音乐播放器,今天实现了歌词动画,不是滚动的,滚动的慢慢研究 思路:在右边放了三个textBlock,设置 textBlock的effect属性 <TextBlock.Effe ...
- k8s rc
RC是用来管理Pod的,每个RC由一个或多个Pod组成:在RC被创建之后,系统将会保持RC中的可用Pod的个数与创建RC时定义的Pod个数一致,如果Pod个数小于定义的个数,RC会启动新的Pod,反之 ...
- epoll好文章
https://www.cnblogs.com/apprentice89/p/3234677.html https://www.jianshu.com/p/aa486512e989 https://c ...
- 微信小程序遇到的问题
1.小程序navigator点击时不显示背景颜色 详情见官方文档:https://developers.weixin.qq.com/miniprogram/dev/component/navigato ...
- 【bzoj 2716】[Violet 3]天使玩偶 (CDQ+树状数组)
题目描述 Ayu 在七年前曾经收到过一个天使玩偶,当时她把它当作时间囊埋在了地下.而七年后 的今天,Ayu 却忘了她把天使玩偶埋在了哪里,所以她决定仅凭一点模糊的记忆来寻找它. 我们把 Ayu 生活的 ...
- 使用git提交代码流程
一.拉取最新代码 一般在本地进行开发时,都是切换到自己的dev分支进行开发,当开发完成需要进行代码提交,在进行代码提交前需要先进行拉取远程仓库代码,进行更新,但是此时会提示需要将本地代码进行commi ...
- Win10内部更新:警告用户别用chrome和Firefox
简评:别和 Chrome 和 Firefox 约行不,我 Edge 明明更美.屁股更翘.更性感... 微软正在测试 Windows 10 的一个更新:警告用户不要安装 Chrome 和 Firefox ...
- vue2.0-router的绑定
1.首先在‘components’文件夹里面创建想要切换的路由. 2.在index.js文件里面引入地址并进行路由的注入. 3.使用<router-link to="path地址&qu ...