黑马程序员技术交流社区
标题:
try {}里有一个return语句,那么紧跟在这个try后的finally {}里...
[打印本页]
作者:
ln0491
时间:
2015-8-29 11:39
标题:
try {}里有一个return语句,那么紧跟在这个try后的finally {}里...
package com;
public class Test1 {
public static void main(String[] args) {
System.out.println(new Test1().test());
}
static int test()
{
int x = 1;
try
{
return x;
}
finally
{
System.out.println(++x);
}
}
}
复制代码
结果:2,1
再如果finally中也有return 结果会什么样
package com;
public class Test1 {
public static void main(String[] args) {
System.out.println(new Test1().test());
}
static int test()
{
int x = 1;
try
{
return x;
}
finally
{
return ++x;
}
}
}
复制代码
结果是:2
看下面这个
package com;
public class Test1 {
public static void main(String[] args) {
System.out.println(new Test1().test());
}
static int test()
{
int x = 1;
try
{
System.out.println("try return ");
return x;
}
finally {
System.out.println("finally return ");
return ++x;
}
}
}
复制代码
结果:
try return
finally return
2
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2