finally語句如何執行
一般是在try…catch…finally中配對使用finally,多用來釋放資源。雖然這個點很簡單,但還是有些地方需要注意的。
- 無論try是否發生異常,finally語句都會執行
- 如果try/catch中包含控制轉移語句(return、continue、break),finally都會在這些控制語句前執行
- 但是像try/catch中有
System.exit(0)
退出JVM,或者Daemon線程退出(也就是線程被中斷,被kill),finally語句都不會執行
public class testFinally{
public static int test(){
try{
Integer.parseInt("execption");
System.out.println("block 0");
}catch (Exception e){
System.out.println("block 1");
//System.exit(0);//取消的話直接退出
return iamReturn();
}finally{
System.out.println("block 2");
return 2;
}
}
public static int iamReturn() {
System.out.println("return block");
return 666;
}
public static void main(String[] args){
System.out.println(test());
}
}
輸出:
block 1
return block
block 2
2
public class testFinally1{
public static int test(){
try{
System.out.println("block 0");
return 1;
}catch (Exception e){
System.out.println("block 1");
//return 2;
}finally{
System.out.println("block 2");
//return 3;
}
return 4;//小心這裏,可能會出現不可達的情況,比如finally中有return,或者try/catch都有return
}
public static void main(String[] args){
System.out.println(test());
}
}
輸出:
block 0
block 2
1
public class testFinally2{
public static int test(){
int i = 0;
try{
return i;
}finally{
i++;//++i也罷
}
}
public static void main(String[] args){
System.out.println(test());
}
}
輸出:
0
這個還是輸出0,不是1,即使finally會先於try種的return執行,這個涉及jvm如何編譯finally語句
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 語句,因爲它們根本就沒有返回值。
這個還是挺秀的,之前都沒注意過,今天看到https://www.ibm.com/developerworks/cn/java/j-lo-finally/index.html提到了就記下來了