Finally的使用总结


//清单一:
public class Test1 {
public static void main(String[] args) {
System.out.println("return value of test(): " + test());
}
public static int test() {
int i = 1;
// if(i == 1)
// return 0;
System.out.println("the previous statement of try block");
i = i / 0;
try {
System.out.println("try block");
return i;
} finally {
System.out.println("finally block");
}
}
}
//结论:如果在try中遇到异常,如果没有catch,则直接退出,不会执行finally
/*执行结果:the previous statement of try block
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.testfinally.sym.Test1.test(Test1.java:12)
at com.testfinally.sym.Test1.main(Test1.java:5)
*/
//清单二:
public class Test2 {

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");
System.exit(0);
return i;
} finally {
System.out.println("finally block");
}
}
}
//结论同上
/*执行结果:try block*/

//清单三:
public class Test3 {
public static void main(String[] args) {
try {
System.out.println("try block");
return;
} finally {
System.out.println("finally block");
}
}
}
//结论:正常情况下,finally先于return执行
/*执行结果:try block
finally block*/

//清单四:
public class Test4 {
public static void main(String[] args) {
System.out.println("reture 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中出现异常,则进入catch;这种情况下三个块中return的优先级:如果、//finally中有return,则函数的返回值就为此return的值;如果finally中没有return,而//catch中有return,则最后的返回值就采用此return的值;如果finally中没有//return,catch中也没有return,则最后的返回值就采用try中的return的值。如果在执行//的时候不需要进入到catch,则不用关注catch中的return值
/*执行结果:try block
exception block
finally block
reture value of test() : 2*/

//清单五:
public class Test5 {

public static void main(String[] args) {
System.out.println("return value of getValue(): " + getValue());
}
public static int getValue() {
try {
return 0;
} finally {
return 1;
}
}
}
//结论同上
/*执行结果:return value of getValue(): 1*/

//清单6:
public class Test6 {

public static void main(String[] args) {

System.out.println("return value of getValue(): " + getValue());
}

public static int getValue() {
int i = 1;
try {
return i;
} finally {
i++;
}
}
}
//结论:Java 虚拟机会把 finally 语句块作为 subroutine(对于这个 subroutine 不知该//如何翻译为好,干脆就不翻译了,免得产生歧义和误解。)直接插入到 try 语句块或者 //catch 语句块的控制转移语句之前。但是,还有另外一个不可忽视的因素,那就是在执行 //subroutine(也就是 finally 语句块)之前,try 或者 catch 语句块会保留其返回值到//本地变量表(Local Variable Table)中。待 subroutine 执行完毕之后,再恢复保留的//返回值到操作数栈中,然后通过 return 或者 throw 语句将其返回给该方法的调用者//(invoker)。请注意,前文中我们曾经提到过 return、throw 和 break、continue 的区//别,对于这条规则(保留返回值),只适用于 return 和 throw 语句,不适用于 break //和 continue 语句,因为它们根本就没有返回值。
/*执行结果:return value of getValue(): 1*/

//清单7

public class Test7 {

public static void main(String[] args) {

System.out.println("return value of getValue(): " + getValue());
}

public static int getValue() {
int i = 1;
try {
i = 4;
} finally {
i++;
return i;
}
}
}
//结论:return在finally中,情况与清单6就不一样了,属于正常返回
/*执行结果:return value of getValue(): 5*/

//清单8
public class Test8 {

public static void main(String[] args) {

System.out.println("return value of getValue(): " + getValue());
}

public static int getValue() {
int i = 1;
try {
i = 4;
} finally {
i++;
}
return i;
}
}
//结论:return在外面也是正常返回
/*执行结果:return value of getValue(): 5*/

//清单9
public class Test9 {

public static void main(String[] args) {

System.out.println(test());
}

public static String test() {
try {
System.out.println("try block");
return test1();
} finally {
System.out.println("finally block");
}
}

public static String test1() {
System.out.println("return statement");
return "after return";
}
}
//结论:return test1(); 这条语句等同于 :
//1. String tmp = test1();
//2. return tmp;
/*执行结果:try block
return statement
finally block
after return*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值