62,634
社区成员




因为throw必须最后执行,而finally又必须执行,为了保证这个规则,所以可能是先执行的try,然后执行catch,发现是throw,就先执行finally里的内容。而且得保证从上至下得执行规则,应该底层代码用了临时变量。类似于i = i++ 这种问题。你说去掉finally就正常了,那说明finally里的内容对throw产生了影响。
public class Test
{
void test() throws Exception
{
try
{
int a = 1/0;
}catch(Exception e){
throw e;
}
finally
{
System.out.println("finally");
}
}
public static void main(String[] args)
{
try
{
new Test().test();
}
catch (Exception e)
{
System.out.println("Do you see me ?");
}
}
}
/*
output:
finally
Do you see me ?
*/
public class Test
{
void test() throws Exception
{
try
{
int a = 1/0;
}catch(Exception e){
throw e;
}
/*finally
{
System.out.println("finally");
}*/
}
public static void main(String[] args)
{
try
{
new Test().test();
}
catch (Exception e)
{
System.out.println("Do you see me ?");
}
}
}
//output:Do you see me ?
public class Test
{
void test() throws Exception
{
try
{
int a = 1/0;
}catch(Exception e){
//throw e;
}
/*finally
{
System.out.println("finally");
}*/
}
public static void main(String[] args)
{
try
{
new Test().test();
}
catch (Exception e)
{
System.out.println("Do you see me ?");
}
}
}
//output:(什么也没有...)
public static int test(){
try{
return 1;
}
catch(Exception e){
}
finally{
return 2;
}
}