如果try中有return ,那么finally执行在return 之前还是之后?
解决方案 »
- 怎么自己写个ABS取绝对值函数阿?
- 初学者问题:怎么改变消息字体的大小?
- 用swing开发、怎样实现分隔线的功能?
- 2000下的cmd和command命令竟然不同!!!!看一下!
- 编写JAVA代码重启Windows 2000?
- 100分求JDK最新版:
- 如何序列化从Sesion Bean返回的ResultSet?我是新手,望各位大哥照顾!!!!
- 如何在方法内部对外部数据进行操作,不用返回值:而外部数据已经被修改。
- 我的Jbuilder 4 foundation 每次启动都会启动我的拼音加加...
- tomcat配置后,可以通过浏览器访问,但是java得HttpURLConnection访问总是失败。
- 谁有Together6.0的注册码 能不能分享一下?
- 正则表达式怎么匹配空格
根据java规范:在try-catch-finally中,如果try-finally或者catch-finally中都有return,则两个return语句都执行并且最终返回到调用者那里的是finally中return的值;而如果finally中没有return,则理所当然的返回的是try或者catch中return的值,但是finally中的代码是必须要执行的。
Core Java有一段这样说的:
CAUTION: A finally clause can yield unexpected results when it contains return statements.
Suppose you exit the middle of a try block with a return statement. Before the method returns,
the contents of the finally block are executed. If the finally block also contains a return statement,
then it masks the original return value. Consider this contrived example:
public static int f(int n)
{
try
{
int r = n * n;
return r;
}
finally
{
if (n == 2) return 0;
}
}
If you call f(2), then the try block computes r = 4 and executes the return statement. However,
the finally clause is executed before the method actually returns. The finally clause
causes the method to return 0, ignoring the original return value of 4.
这说明try clause中想返回的值被覆盖了