JAVA异常处理之finally中最好不要使用return
finally 语句块中, 最好不要使用return, 否则会造成已下后果;
1, 如果catch块中捕获了异常, 并且在catch块中将该异常throw给上级调用者进行处理, 但finally中return了, 那么catch块中的throw就失效了, 上级方法调用者是捕获不到异常的. 见demo如下:
public class TestException {
public TestException() {
}
boolean testEx() throws Exception {
boolean ret = true;
try {
ret = testEx1();
} catch (Exception e) {
System.out.println("testEx, catch exception");
ret = false;
throw e;
} finally {
System.out.println("testEx, finally; return value=" + ret);
return ret;
}
}
boolean testEx1() throws Exception {
boolean ret = true;
try {
ret = testEx2();
if (!ret) {
return false;
}
System.out.println("testEx1, at the end of try");
return ret;
} catch (Exception e) {
System.out.println("testEx1, catch exception");
ret = false;
throw e;
} finally {
System.out.println("testEx1, finally; return value=" + ret);
return ret;
}
}
boolean testEx2() throws Exception {
boolean ret = true;
try {
int b = 12;
int c;
for (int i = 2; i >= -2; i--) {
c = b / i;
System.out.println("i=" + i);
}
return true;
} catch (Exception e) {
System.out.println("testEx2, catch exception");
ret = false;
throw e;
} finally {
System.out.println("testEx2, finally; return value=" + ret);
return ret;
}
}
public static void main(String[] args) {
TestException testException1 = new TestException();
try {
testException1.testEx();
} catch (Exception e) {
e.printStackTrace();
}
}
}
该方法的最终执行结果如下:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, finally; return value=false
testEx, finally; return value=false
尽管testEx2中的catch抛出异常, 但因为finally中return了, 那么testEx1中是捕获不到异常的, 所以testEx1中的catch块是不会执行的.
2, 如果在finally里的return之前执行了其它return , 那么最终的返回值是finally中的return:
public class TestException3 {
public static int x = 3;
int testEx() throws Exception {
try {
x = 4;
return x;
} catch (Exception e) {
} finally {
x = 5;
return x;
}
}
public static void main(String[] args) {
TestException3 testException3 = new TestException3();
try {
int a = testException3.testEx();
System.out.println(a);
System.out.println(x);
} catch (Exception e) {
}
}
}
输出结果为5, 5
如果finally中的return被注释掉, 那么输出结果是4, 5
JAVA异常处理之finally中最好不要使用return的更多相关文章
- java异常处理:finally中不要return
java异常处理:finally中不要return 复制代码 public class Ex1 { public static void main(String[] args) { System.ou ...
- 项目中java异常处理
一.java异常类介绍. Throwable: 有两个重要的子类:Exception(异常)和 Error(错误),二者都是 Java 异常处理的重要子类,各自都包含大量子类. 有一篇比较好的blog ...
- java 8 lambda表达式中的异常处理
目录 简介 处理Unchecked Exception 处理checked Exception 总结 java 8 lambda表达式中的异常处理 简介 java 8中引入了lambda表达式,lam ...
- Java异常处理场景中不同位置的返回值详细解析
Java 异常处理中的返回值在不同位置不同场景下是有一些差别的,这里需要格外注意 具体分以下两种场景: 1 finally语句块没有return语句,即当代码执行到try或者catch语句块中的ret ...
- java异常处理-finally中使用return和throw语句
java异常语句中的finally块通常用来做资源释放操作,如关闭文件.关闭网络连接.关闭数据库连接等.正常情况下finally语句中不应该使用return语句也不应该抛出异常,以下讨论仅限于java ...
- JAVA异常处理分析(中)
在使用java异常处理机制时候我们会发现有些异常抛出后可以不需要进行抓取处理,而有些异常必须要进行抓取处理,这是个什么情况呢? 设计理念猜想: 有一些场景的异常,是可以不需要处理或是经常不会 ...
- 札记:Java异常处理
异常概述 程序在运行中总会面临一些"意外"情况,良好的代码需要对它们进行预防和处理.大致来说,这些意外情况分三类: 交互输入 用户以非预期的方式使用程序,比如非法输入,不正当的操作 ...
- java异常处理(父子异常的处理)
我当初学java异常处理的时候,对于父子异常的处理,我记得几句话“子类方法只能抛出父类方法所抛出的异常或者是其子异常,子类构造器必须要抛出父类构造器的异常或者其父异常”.那个时候还不知道子类方法为什么 ...
- Java 异常处理
异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误java.lang.Error:如果你用System.out ...
随机推荐
- 二分图最小路径覆盖--poj2060 Taxi Cab Scheme
Taxi Cab Scheme 时间限制: 1 Sec 内存限制: 64 MB 题目描述 Running a taxi station is not all that simple. Apart f ...
- MyElipse配置
DK1.6.0+Tomcat6.0+myEclipse的安装配置 C:\Users\Administrator\AppData\Local\Genuitec\Pulse Explorer JDK1.6 ...
- 遇到ANDROID “call to opengl es api with no current context”错误
延迟线程执行 Timer timer=new Timer();//实例化Timer类 timer.schedule(new TimerTask(){ public void run(){ buyed( ...
- 使用Scribefire在博客中插入语法高亮 II
效果如下, 这是我们在Scribefire中添加的code按钮,单击此按钮,则会出现 在codeHere中直接输入代码就可以了. 查看html 可以看到,其中已经添加了<pre>标签. 下 ...
- Vijos 1025 小飞侠的游园方案 0-1背包
描述 经过抽签选择,小智将军第一个进入考场. 菜虫:(身上散射出华贵(?)的光芒)欢迎你,第一位挑战者!! 小智:--(走到菜虫身后,关灯)女王陛下,虽然我们国家现在很富裕,但也请您不要浪费电来用这么 ...
- Linux配置LNMP环境(三)配置MySQL
1.执行代码:cd /usr/local/rsc下载MySQL,我是从搜狐镜像上下载的:http://mirrors.sohu.com/mysql/MySQL-5.5/,我下载的是64位(注意)的,下 ...
- <jsp:include>和<%@include%>的区别
个人笔记(并非自己总结,而是从别人的博客上看到的) <jsp:include> :动态包含 1.<jsp:include>包含的是html文件 举例: DynamicInclu ...
- web.xml is missing and <failOnMissingWebXml> is se
摘要 maven模块化 在学习maven模块化构建项目的时候遇到了如下报错信息: web.xml is missing and <failOnMissingWebXml> is set t ...
- iOS内购(IAP)中的那些坑
公司的公共库原来并没有这部分的代码,以前做内购是用两个比较有名的github上的第三方库.一个叫MKStoreKit,另一个叫IAPManager,我看了一下写的都很辣鸡,使用起来很不方便,而且写的还 ...
- 理解Linux文件系统之inode
很少转发别人的文章,但是这篇写的太好了. 理解inode 作者: 阮一峰 inode是一个重要概念,是理解Unix/Linux文件系统和硬盘储存的基础. 我觉得,理解inode,不仅有助于提高系统 ...