標籤:
finally 子句(clause)是不是總會執行???
package com.volshell.test;public class Main { public static void main(String[] args) { change1(10); } private static void change1(int word) { System.out.println("測試結果:" + test(1)); } private static int test(int i) { if (i == 1) return 0; System.out.println("將要進入try塊"); try { System.out.println("try block"); return 3; } catch (Exception e) { // TODO: handle exception } finally { System.out.println("finally"); i++; return i; } }}
上面結果為 測試結果:0
第一條:如果沒有進入try中,是不會執行finally子句的。
1 package com.volshell.test; 2 3 public class Main { 4 public static void main(String[] args) { 5 change1(10); 6 } 7 8 private static void change1(int word) { 9 System.out.println("測試結果:" + test(1));10 }11 12 private static int test(int i) {13 // if (i == 1)14 // return 0;15 System.out.println("將要進入try塊");16 try {17 System.out.println("try block");18 System.exit(0);19 return 3;20 } catch (Exception e) {21 // TODO: handle exception22 } finally {23 System.out.println("finally");24 i++;25 return i;26 }27 }28 }
測試結果:將要進入try塊
try block
這次同樣沒有進入finally中。因為在try中調用了System.exit(0);
第二條:當一個線程正在執行try語句塊或者catch語句塊的時候,突然被打斷或者終止,那麼相應的finally是不會被執行的。
***********************************************************************************************************
The finally Block
The finally block always executes when the try block exits. This ensures that the finally
block is executed even if an unexpected exception occurs. But finally is useful for
more than just exception handling — it allows the programmer to avoid having cleanup
code accidentally bypassed by a return,continue, or break. Putting cleanup code in a
finally block is always a good practice, even when no exceptions are anticipated.
Note: If the JVM exits while the try or catch code is being executed, then the finally
block may not execute. Likewise, if the thread executing the try or catch code is
interrupted or killed, the finally block may not execute even though the application
as a whole continues.
***************************************************************************************************************
關於try,catch,finally的執行順序的問題:
***************************************************************************************************************
where either at least one catch clause, or the finally clause, must be present. 至少有一個catch或者finally子句。可以沒有catch
The body of the try statement is executed until either an exception is thrown or the body
finishes successfully. ---異常拋出或者程式正確執行都會執行try.
If an exception is thrown, each catch clause is examined in turn,
from first to last, to see whether the type of the exception object is assignable to
the type declared in the catch. When an assignable catch clause is found, its block
is executed with its identifier set to reference the exception object. No other catch
clause will be executed. Any number of catch clauses, including zero, can be associated
with a particular Try as long as each clause catches a different type of exception.
If no appropriate catch is found, the exception percolates (滲透)out of the try statement
into any outer try that might have a catch clause to handle it.--如果沒有找到合適的捕獲,交由外面的捕獲來處理。
If a finally clause is present with a try, its code is executed after all other processing
in the try is complete. This happens no matter how completion was achieved, whether
normally, through an exception, or through a control flow statement such as return or
break.只有處理完畢try中的語句(不包括異常語句,return語句,break語句)之後,才會執行finally全部子句(包括其中的return子句).
***************************************************************************************************************
第三條:只有處理完畢try中的語句(不包括異常語句,return語句,break語句)之後,才會執行finally全部子句(包括其中的return子句等)。處理完finally之後再返回去處理try中的剩餘子句。
Java--finally