finally段究竟有没有执行?

pengchengjiji 2010-09-12 08:42:26
public class Demo_1{
public static void main (String[] args){
System.out.println (" the value of i is" + new Demo_1().test());
}
private int test(){
int i = 1;
try{
return i;
}finally{
++i ;
System.out.println ("fianlly is Executed");
}

}
}
你们先不编译,看看输出什么?
...全文
180 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
dengqibin 2010-09-13
  • 打赏
  • 举报
回复
http://blog.csdn.net/dengqibin/archive/2010/06/23/5690442.aspx
这个是我以前写的异常处理流程,可以看看
db_cwade 2010-09-12
  • 打赏
  • 举报
回复
看来没有回的必要了。都 说得很清楚
zhuyouyong 2010-09-12
  • 打赏
  • 举报
回复
顶[Quote=引用 1 楼 ituren 的回复:]
finally会在return语句前执行.
所以会输出"fianlly is Executed".
[/Quote]
pengchengjiji 2010-09-12
  • 打赏
  • 举报
回复

public class Demo_1{
public static void main (String[] args){
System.out.println (" the value of i is--> " + new Demo_1().test());
}
public int test(){
int i = 1;
try{
return i;
}finally{
++i ;
System.out.println ("fianlly is Executed");
return ++i;
}
}
}

输出
fianlly is Executed
the value of i is--> 3

18楼的讲的没错
pengchengjiji 2010-09-12
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 ticmy 的回复:]

try {
// ... do something ...
return 1;
} finally {
return 2;
}



When the TRy block executes its return, the finally block is entered with the "reason" of returning the value 1……
[/Quote]
没错!我终于明白了

龙四 2010-09-12
  • 打赏
  • 举报
回复
try {
// ... do something ...
return 1;
} finally {
return 2;
}



When the TRy block executes its return, the finally block is entered with the "reason" of returning the value 1. However, inside the finally block the value 2 is returned, so the initial intention is forgotten. In fact, if any of the other code in the try block had thrown an exception, the result would still be to return 2. If the finally block did not return a value but simply fell out the bottom, the "return the value 1" reason would be remembered and carried out.




——摘自《THE Java™ Programming Language, Fourth Edition》By Ken Arnold, James Gosling, David Holmes
ituren 2010-09-12
  • 打赏
  • 举报
回复
http://zangxt.javaeye.com/blog/421508
可以看下这里,讲的比较详细...
主要就是压栈的问题.
haoll714773361 2010-09-12
  • 打赏
  • 举报
回复
顶五楼 ++i会执行但不返回 返回的是开始的i值 在main中输出的是开始时返回的值
龙四 2010-09-12
  • 打赏
  • 举报
回复
public class Demo_1{
public static void main (String[] args){
System.out.println (" the return value is" + new Demo_1().test());
}
private int test(){
int i = 1;
try{
return i;
}finally{
++i ;
System.out.println (" the value of i is " + i);
System.out.println ("fianlly is Executed");
}

}
}
pengchengjiji 2010-09-12
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 huyong479072052 的回复:]

try 中碰到return语句 会跳出try 查看是否有final语句,有则执行,无则返回,
所以final执行了,所以结果为:fianal is Executed
the value of i is 1;
[/Quote]
怎么理解?那个临时变量是什么来的?是java机制里面的吗?
龙四 2010-09-12
  • 打赏
  • 举报
回复
是返回值是1,不是i的值是1吧

[Quote=引用 10 楼 pengchengjiji 的回复:]

引用 8 楼 huyong479072052 的回复:
try 中碰到return语句 会跳出try 查看是否有final语句,有则执行,无则返回,
所以final执行了,所以结果为:fianal is Executed
the value of i is 1;

但是i的值怎么还是1?
[/Quote]
pengchengjiji 2010-09-12
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ituren 的回复:]

厄.. 没看清楚.. finally里还有个++i;
还要考虑i=几的问题..
[/Quote]
i++的确执行了
huyong479072052 2010-09-12
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ticmy 的回复:]

++i执行了
不信在++i后面打印一下i

只不过return语句已经确定了值了,应该放到了一个临时变量里,后面的i改变对于这个临时变量没有影响
[/Quote]
恩 return已经有了确定的值i=1了,然后等待执行,所以后面的++i 虽然执行了但是return值并不改变。
pengchengjiji 2010-09-12
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 huyong479072052 的回复:]
try 中碰到return语句 会跳出try 查看是否有final语句,有则执行,无则返回,
所以final执行了,所以结果为:fianal is Executed
the value of i is 1;
[/Quote]
但是i的值怎么还是1?
龙四 2010-09-12
  • 打赏
  • 举报
回复
++i执行了
不信在++i后面打印一下i

只不过return语句已经确定了值了,应该放到了一个临时变量里,后面的i改变对于这个临时变量没有影响
huyong479072052 2010-09-12
  • 打赏
  • 举报
回复
try 中碰到return语句 会跳出try 查看是否有final语句,有则执行,无则返回,
所以final执行了,所以结果为:fianal is Executed
the value of i is 1;
sound9world 2010-09-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 pengchengjiji 的回复:]

引用 4 楼 sound9world 的回复:
++i 不会执行的吧. 打印语句应该会执行

fianlly is Executed
the value of i is 1

我想知道的就是 ++i 为什么不会执行,后面的打印反而执行
我有一个理解是return已经把函数的值返回去了,再去执行打印语句,其实i的值已经改变了,只是没有打印出来而已,对不对?请赐教
[/Quote]
我认为可以这样理解,那个++i是会被执行的 但是不会被返回
你可以再finally中打印i的值
pengchengjiji 2010-09-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sound9world 的回复:]
++i 不会执行的吧. 打印语句应该会执行

fianlly is Executed
the value of i is 1
[/Quote]
我想知道的就是 ++i 为什么不会执行,后面的打印反而执行
我有一个理解是return已经把函数的值返回去了,再去执行打印语句,其实i的值已经改变了,只是没有打印出来而已,对不对?请赐教
sound9world 2010-09-12
  • 打赏
  • 举报
回复
错了..++i会执行..但不会被return
sound9world 2010-09-12
  • 打赏
  • 举报
回复
++i 不会执行的吧. 打印语句应该会执行

fianlly is Executed
the value of i is 1

加载更多回复(3)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧