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的原因.下面 ...
随机推荐
- linux 流量统计
因为很多vps或者服务器都是限流量的,但是又很多服务商并没有提供详细的流量表,比如每天的流量表,所以肯定有人很想知道自己服务器到底跑了多少流量. vnstat就是一个很好用的服务器流量统计命令.我截几 ...
- Linux 用户和文件
Linux系统中用户的扩展研究 进程 用户和文件 Linux中的用户及用户组 linux中只有两个等级:root和非root, 一个用户至少属于一个用户组 一个用户可以属于多个用户组 用户本身的区别主 ...
- [51单片机] nRF24L01 无线模块 串口法命令 通过无线控制另一个的灯
>_<!概述: 这是在上一个的基础上通过按键发送4种不同命令来控制接收端的LED灯亮的改进版(上一个:http://www.cnblogs.com/zjutlitao/p/3840013. ...
- 7-Linq查询语言
本篇博客对应视频讲解 回顾 上一期我们讲解了字符编码相关的内容,大家应该去理解字符和字节之间的关系.并学习使用Encoding类进行编码之间的转换. 今天给大家讲的内容十分的重要,也是C#等语言比其他 ...
- python--内置模块(二) os sys pickle json
1.os模块 常用方法: os.makedirs('dirname1/dirname2') 可生成多层递归目录 os.removedirs('dirname1') 若目录为空,则删除,并递归到上一级目 ...
- python redis 方法大全
redis连接 1,安装redis pip install redis 实例: import redis from datetime import datetime r = redis.Redis(h ...
- SQLAlchemy和Flask-SQLAlchemy
一.ORM 与 SQLAlchemy 简介 ORM 全称 Object Relational Mapping, 翻译过来叫对象关系映射.简单的说,ORM 将数据库中的表与面向对象语言中的类建立了一种对 ...
- 前端必学内容:webpack3快速入门 1-23节内容参考
前端必学内容:webpack(模块打包器) webpack3 学习内容,点击即可到达 (1).webpack快速入门——如何安装webpack及注意事项 (2).webpack快速入门——webpac ...
- [ActionScript 3.0] as3启动QQ
import flash.html.HTMLLoader; import flash.net.URLLoader; import flash.net.URLRequest; import flash. ...
- docker微服务部署之:一,搭建Eureka微服务项目
先说明一下docker需要搭建的微服务的基本情况: 项目情况:一个demo_parent项目,下面三个子模块:demo_eureka(eureka服务).demo_article(文章服务).demo ...