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的原因.下面 ...
随机推荐
- Android阻止AlertDialog关闭
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle("测试" ...
- 重新打造的我的Pugo
Pugo博客已经搭建了好几个月了吧,给我感受到非常方便的就是,我换了好多VPS,迁移显得非常的方便,但是也有不足的地方,比如发布一篇新的博客,我每次都需要重新进入后台进行新的Post,还需要重新bui ...
- 【cocos2d-x 手游研发小技巧(5)获取网络图片缓存并展示】
今天是年前最后一天上班了,最后一天上班,祝大家马上有各种东西,最后一天也给写一点干货,就是获取网络图片: 经过自己简单封装了一下,实现了获取网络图片,按照比例展示出来,实现方法是cocos2dx - ...
- ASP.NET Core使用Ping判断网络是否接通
static void Main(string[] args) { // 主机地址 string targetHost = "bing.com"; string data = &q ...
- java中-的流-与操作
/* 字节输出流 OutputStrema: * OutputStream抽象类 * write(int b); 将指定的字节写入此流中 * write(byte[] b); ...
- WebStorm 简单部署服务器进行测试操作
WebStorm 简单部署服务器对外发布接口 第一步: 查看webstorm防火墙是否允许链接,控制面板-->防火墙-->高级设置 入站规则-->webstrom是否允许链接 ,双击 ...
- drools入门示例
Drools是一个基于java的规则引擎,开源的,可以将复杂多变的规则从硬编码中解放出来,以规则脚本的形式存放在文件中,使得规则的变更不需要修正代码重启机器就可以立即在线上环境生效.随着互联网金融的兴 ...
- bzoj4842: [Neerc2016]Delight for a Cat
bzoj4842 这是一道网络流的题(大家都看出来了吧) 首先我们简化一下题目,选出最关键的部分(就是知道什么和要求什么,还有条件) 我们在这里把睡觉设为0,至少有t0时间在睡觉,把打隔膜设为1,至少 ...
- 一次完整的http请求全程
当我们在打开浏览器的时候,在地址栏输入诸如 http://www.baidu.com时,几秒后浏览器打开百度页面,几秒钟内到底发生了哪些事情. 一.解析URL: 浏览器首先会对输入的URL进行检查,如 ...
- Tensorflow入门----占位符、常量和Session
安装好TensorFlow之后,开一个python环境,就可以开始运行和使用TensorFlow了. 先给一个实例, #先导入TensorFlow import tensorflow as tf he ...