public class ExceptionPitfall { public static void main(String[] args) { try { throw new RuntimeException(); } finally { return; } } }
输出为空,没有报错,这时好像吞掉了Exception
所以最好不要在finally中使用return
public class FinallyTest
{
public static void main(String[] args) {
System.out.println(new FinallyTest().test());;
}
static int test()
{
int x = 1;
try
{
x++;
return x;
}
finally
{
++x;
}
}
}
输出2
finally中改变了x中的值,但是返回的确实try中值,