- 浏览: 49754 次
文章分类
最新评论
之前在博客中有一篇文章讨论过异常中return值的情况,有兴趣可以参见http://gaofulai1988.iteye.com/admin/blogs/2259371,当时的例子比较简单,今天来了特殊点的例子。
在上例子之前,还是回顾以下之前的几个要点:
1. throw 后面的代码是不会执行的。
2. 不管是否有异常,都会执行finally。
3. 不管有多少个return, 只会执行finally里的return。
此时的答案是多少呢?
进入了finally里了,但是不是2,是1?! 不可思议!看来只能找Oracle来帮我们解答了。
If the try clause executes a return, the compiled code does the following:
Saves the return value (if any) in a local variable.
Executes a jsr to the code for the finally clause.
Upon return from the finally clause, returns the value saved in the local variable.
The compiler sets up a special exception handler, which catches any exception thrown by the try clause. If an exception is thrown in the try clause, this exception handler does the following:
Saves the exception in a local variable.
Executes a jsr to the finally clause.
Upon return from the finally clause, rethrows the exception.
http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.10.2.5
现在明白了吧,进入到finally里后的操作其实没有什么用,因为之前的变量已经被保存起来了。
在上例子之前,还是回顾以下之前的几个要点:
1. throw 后面的代码是不会执行的。
2. 不管是否有异常,都会执行finally。
3. 不管有多少个return, 只会执行finally里的return。
public static int doexception(){ int x=0; try{ throw new Exception("aaa"); }catch(Exception e){ System.out.println("catch exception"); return ++x; }finally{ System.out.println("finally ...."); ++x; } } public static void main(String args[]){ System.out.println("return value:"+doexception()); }
此时的答案是多少呢?
catch exception finally .... return value:1
进入了finally里了,但是不是2,是1?! 不可思议!看来只能找Oracle来帮我们解答了。
引用
If the try clause executes a return, the compiled code does the following:
Saves the return value (if any) in a local variable.
Executes a jsr to the code for the finally clause.
Upon return from the finally clause, returns the value saved in the local variable.
The compiler sets up a special exception handler, which catches any exception thrown by the try clause. If an exception is thrown in the try clause, this exception handler does the following:
Saves the exception in a local variable.
Executes a jsr to the finally clause.
Upon return from the finally clause, rethrows the exception.
现在明白了吧,进入到finally里后的操作其实没有什么用,因为之前的变量已经被保存起来了。
发表评论
-
Java IO 读文件的各种方法总结
2016-01-01 15:00 663IO分为字节流和字符流,字符就是简单的字符串存储,从理伦上讲, ... -
动态代理的应用
2015-12-22 17:30 689代理模式作为开发人员 ... -
Java Restful
2015-12-19 14:01 393对于两个系统之间交互信息,有两种常见的方式:webservic ... -
request.getInputStream() 只能读一次的解决方法
2015-12-17 12:17 2290我们知道request.getInputStream()只能读 ... -
java Hessian 版本冲突问题解决方法
2015-12-11 19:44 818今天在实际的项目发现了一个问题就是hessian的版本不兼容的 ... -
ThreadPoolExecutor参数讲解
2015-12-10 08:14 7751. 线程池可以节省创建多个线程带来的开销问题。 2. 线程 ... -
Java RSA 加密 解密 签名 验签
2015-12-09 10:01 58641. 加密的作用 1)明文变密文(你不知道密钥是很难解密的) ... -
Java Xstream xml 与bean之间的转换
2015-12-09 08:31 691xml文件如下: <mvc> & ... -
XPATH 解析XML
2015-12-09 08:28 3971. 表达式描述 nodename 选取此节点的所有子节 ... -
Java Dom4j 解析XML
2015-12-09 08:23 326Dom4j和JDom是很相似的,用起来十分方便。 XML文件 ... -
Java JDom 解析xml
2015-12-09 08:22 359JDOM在解析XML在代码量之上比之前的方法(DOM和SAX要 ... -
Java SAX 解析xml
2015-12-08 18:13 360在上一篇中http://gaofulai1988.iteye. ... -
Java XML解析系列
2015-12-08 18:00 678Java解析XML有多种方式,因此需要分为几个不同的系列来讲。 ... -
Java 背包算法计算从数组中找若干个数使其最接近某个数
2015-12-08 17:38 1931背包的算法的动态方式如下: f(i,w) = max{ f(i ... -
C3P0 连接分析
2015-12-01 19:05 850最近在看C3P0的原理,还是将C3P0的源码导入到Ecplis ... -
微信开发的原理
2015-11-30 10:10 1275微信在现在的生活中,扮演着举足轻重的角色,现在怎么东西都在微信 ... -
JAVA Timestamp 与Data的转化以及BigDecimal 保留两位小数
2015-11-27 14:47 15941. BigDecimal 保留两位小数 今天在项目中遇到这 ... -
Java JDBC executeBatch 批量操作
2015-11-27 08:05 1545对JDBC 的 CRUD操作,我相信对于每个开发人员来讲,是十 ... -
Java WeakHashMap 分析
2015-11-26 08:17 577昨天在我们的系统中看 ... -
加密与解密
2015-11-18 18:12 438我本身不是学密码出身的,但在工作中经常要使用加密与解密的东东, ...
相关推荐
Condition 3: try中有异常,try-catch-finally里都没有return ,finally 之后有个returntry中有异常以后,根据
在Java的异常机制中,如果finally中含有return语句,则try和catch中的return语句将会被JVM忽视
try、catch、finally、return 执行顺序超详细讲解,包看包会。
第二:finally里面不建议放return语句,根据需要,return语句可以放在try和catch里面和函数的后。可行的做法有四: 1、return语句只在函数后出现一次。 2、return语句仅在try和catch里面都出现。 3、...
Java finally语句到底是在return之前还是之后执行?Java finally执行深度剖析,具体看这篇博文:http://blog.csdn.net/lanxuezaipiao/article/details/16922895,这是里面相关的源码,欢迎大家下载使用。
try-catch-finally执行顺序验证(左边是.java文件,右边是.class文件) 提示: try、catch块内的return操作编译后会变成把return的值保存到变量var的操作。 总结: try、catch块内的return操作编译后会变成把return的值...
我们知道return语句用在某一个方法中,一是用于返回函数的执行结果,二是用于返回值为void类型的函数中,仅仅是一个return语句(return ;),此时用于结束方法的执行,也即此return后的语句将不会被执行,当然,这种...
The production IfStatement : if ( Expression ) Statement else Statement is evaluated as follows:.
首先执行try,如果有异常执行catch,无论如何都会执行finally,当有return以后,函数就会把这个数据存储在某个位置,然后告诉主函数,我不执行了,接下来你执行吧,所以函数就会推出
finally 一定会被执行,如果 finally 里有 return 语句,则覆盖 try/catch 里的 return , 比较爱考的是 finally 里没有 return 语句,这时虽然 finally 里对 return 的值进行了修改,但 return 的值并不改变这种...
8. try-catch-finally 中,如果 catch 中 return 了,finally 还会执行吗? 9. 类 ExampleA 继承 Exception,类 ExampleB 继承ExampleA。 10. 常见的 RuntimeException 有哪些? 11. Java常见异常有哪些 四、并发...
实验指导书 实验名称:包、接口与异常处理 学时安排:2 实验类别:综合设计型实验 实验要求:1人1组  ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ 一、实验目的 1、掌握java字符串处理...
主要介绍了关于java中的一道面试题,这套题就是在try中含return语句时代码的执行顺序,这个问题看似简单,却暗藏杀机啊!文中通过一个个例子详细介绍了其中玄机,需要的朋友可以参考学习,下面来一起看看吧。
Java计算文件MD5值(支持大文件) package com.hthl.xxtd; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.security.MessageDigest; import org....
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io....
这些关键字包括数据类型关键字(如int、float、char等)、流程控制关键字(如if、else、while等)、访问控制关键字(如public、private、protected等)、异常处理关键字(如try、catch、finally等)以及其他一些特殊...
import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; import ...
一:Try………Catch………finally………end try 捕获错误 把一个可能出错的语句放在try后面,如果出错,执行catch语句, catch可以有多个,第一个catch不能捕获的错误,将被下一个catch语句 所捕获。在所有的处理...
import java.awt.image.BufferedImage; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io....
异常可以防止出现一些不友好的信息返回给用户,有助于提升程序的可用性,在java中通过try … catch … finally来处理异常,在Python中通过try … except … else来处理异常 一、以ZeroDivisionError为例,处理分母为...