try {}里有一个return 语句 那么紧跟在这个try 后的finally {}里的code会不会被执行 什么时候
语句
已帮助 人
try {}里有一个return 语句,那么紧跟在这个try 后的
finally {}里的code会不会被执行,什么时候被执行,在return
前还是后?
-
正确答案:
会执行,在return 前执行。
我的答案是在return中间执行,参看下一题的讲解。
public class Test {
/**
* @param args add by zxx ,Dec 9, 2008
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(new Test().test());;
}
static int test()
{
int x = 1;
try
{
return x;
}
finally
{
++x;
}
}
}
--------执行结果 ---------
1
