【转载自:http://blog.csdn.net/jiasanshou/article/details/6996932】
主要通过几个例子来说明finally语句的执行,至于具体的原因,有待探讨。
开始的(1)(2)(3)例子是为了说明finally语句块不一定被执行。
(1)在try语句块之前有return,没有执行try,也就没有执行finally
package finally_test;
public class test1
{
public static void main(String[] args)
{
//System.out.println("hello world!");
System.out.println("return value of test():"+test());
}
//在try语句块之前遇到return返回调用test()的函数,没有执行try
public static int test()
{
int i=1;
System.out.println("the previous statement of try block");
//return 0;
//这里不能直接return 0;否则会报错,因为直接造成后面的语句没有执行
if(i==1)
{
return 0;
}
//这里采用if,如果释放内存的操作放在之后,也就不会被执行,造成内存泄露,当然指的是C/C++
try
{
System.out.println("try block");
return i;
}
finally
{
System.out.println("finally block");
}
}
}
运行结果:
the previous statement of try block
return value of test():0
package finally_test;
public class test2
{
public static void main(String[] args)
{
//System.out.println("hello world!");
System.out.println("return value of test():"+test());
}
public static int test()
{
int i=1;
System.out.println("the previous statement of try block");
i=i/0;
//try之前遇到其他异常,提前退出程序
try
{
System.out.println("try block");
return i;
}
finally
{
System.out.println("finally block");
}
}
}
运行结果:
the previous statement of try block
Exception in thread "main" java.lang.ArithmeticException: / by zero
at finally_test.test2.test(test2.java:14)
at finally_test.test2.main(test2.java:8)
(3) 在 try 语句块中执行了 System.exit (0) 语句,终止了 Java 虚拟机的运行,也就不执行finally语句块
package finally_test;
public class test3
{
public static void main(String[] args)
{
//System.out.println("hello world!");
System.out.println("return value of test():"+test());
}
public static int test()
{
int i=1;
try
{
System.out.println("try block");
System.exit(0);
return i;
}
finally
{
System.out.println("finally block");
}
}
}
运行结果:
try block
(4)很正常的例子。
package finally_test;
public class test4
{
public static void main(String[] args)
{
try
{
System.out.println("try block");
return;
}
finally
{
System.out.println("finally block");
}
}
}
运行结果:
try block
finally block
(5)注意,在Eclipse里,在int i=1;这一行会警告“the value of the local variable i is not used”
package finally_test;
public class test5
{
public static void main(String[] args)
{
System.out.println("return value of test():"+test());
}
public static int test()
{
int i=1;
try
{
System.out.println("try block");
i=1/0;
return 1;
}
catch(Exception e)
{
System.out.println("exception block");
return 2;
}
finally
{
System.out.println("finally block");
}
}
}
运行结果:
try block
exception block
finally block
return value of test():2
(6)注意,在Eclipse里,会提示"finally block does not complete normally",具体原因见【 http://blog.csdn.net/woshixuye/article/details/8529447】,就是说在finally语句块中不应该有return语句。
package finally_test;
public class test6
{
public static void main(String[] args)
{
//System.out.println("hello world!");
System.out.println("return value of test():"+test());
}
public static int test()
{
try
{
return 0;
}
finally
{
return 1;
}
}
}
运行结果:
return value of test():1
(7)这个例子非常变态!详细解释见原来的链接。
package finally_test;
public class test7
{
public static void main(String[] args)
{
//System.out.println("hello world!");
System.out.println("return value of test():"+test());
}
public static int test()
{
int i=1;
try
{
//i=4;
return i;
}
finally
{
++i;
}
}
}
运行结果:
return value of test():1
package finally_test;
public class test8
{
public static void main(String[] args)
{
//System.out.println("hello world!");
System.out.println("return value of test():"+test());
}
public static int test()
{
int i=1;
try
{
i=4;
}
finally
{
++i;
//return i;
}
return i;
}
}
运行结果:
return value of test():5
(9)这个例子也非常变态!
package finally_test;
public class test9
{
public static void main(String[] args)
{
//System.out.println("hello world!");
System.out.println(test());
}
public static String test()
{
try
{
System.out.println("try block");
return test1();
//return test1();等同于String tmp=test1(); return tmp;
}
finally
{
System.out.println("finally block");
}
}
public static String test1()
{
System.out.println("return statement");
return "after return";
}
}
运行结果:
try block
return statement
finally block
after return
(10)这个例子和(9)差不多
package finally_test;
public class test10
{
public static void main(String[] args)
{
//System.out.println("hello world!");
System.out.println(test());
}
public static int test()
{
try
{
System.out.println("try block");
int x=1;
return test1(x);
//先执行test1(x);再执行finally,最后return
}
finally
{
System.out.println("finally block");
}
}
public static int test1(int x)
{
System.out.println("return statement");
x++;
return x;
}
}
运行结果:
try block
return statement
finally block
2