2010-08-31 133 views
0

我有關於try,catch和最後在Java中的問題。考慮以下情形:Java中的異常處理

try{ 
//Some code here that throws IOExceotion 
} 
catch (IOException ex){ 
System.out.println("Line 1: IOException Encountered!"); 
throw ex; 
} 
finally { 
System.out.println("Line 2: I am always executed!"); 
} 

上面的代碼段的輸出是什麼? 我要去看看:

Line 1: IOException Encountered! 
Line 2: I am always executed! 

還是會

Line 2: I am always executed! 
Line 1: IOException Encountered! 

或將它只是(因爲我們在catch塊一丟)

Line 1: IOException Encountered! 

基本上,我還沒有找到一個例子,在catch塊中有一個「throw」,最後是catch塊之後的塊(就像上面的例子)。任何人都可以點亮它嗎?

謝謝。

+6

在你花時間寫你的問題,你可以嘗試它爲你自己,當然。 – skaffman 2010-08-31 21:39:30

+0

聽起來像測試... – 2010-08-31 21:40:26

+0

你爲什麼不試試自己? – 2010-08-31 21:44:38

回答

3

你會看到第一個。最後,塊始終執行並作爲最後一個。

2

這是一個流行測驗嗎?開玩笑。如果有異常,你會看到第一個。 finally語句總是會被執行,所以會一直打印。

5

引述Java Language Specification, §14.20.4

一個try語句具有finally塊,先執行try塊執行。然後有一個選擇:

* If execution of the try block completes normally, then the finally block is executed, and then there is a choice: 
     o If the finally block completes normally, then the try statement completes normally. 
     o If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S. 
* If execution of the try block completes abruptly because of a throw of a value V, then there is a choice: 
     o If the run-time type of V is assignable to the parameter of any catch clause of the try statement, then the first (leftmost) such catch clause is selected. The value V is assigned to the parameter of the selected catch clause, and the Block of that catch clause is executed. Then there is a choice: 
      + If the catch block completes normally, then the finally block is executed. Then there is a choice: 
        # If the finally block completes normally, then the try statement completes normally. 
        # If the finally block completes abruptly for any reason, then the try statement completes abruptly for the same reason. 
      + If the catch block completes abruptly for reason R, then the finally block is executed. Then there is a choice: 
        # If the finally block completes normally, then the try statement completes abruptly for reason R. 
        # If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded). 
     o If the run-time type of V is not assignable to the parameter of any catch clause of the try statement, then the finally block is executed. Then there is a choice: 
      + If the finally block completes normally, then the try statement completes abruptly because of a throw of the value V. 
      + If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and the throw of value V is discarded and forgotten). 
* If execution of the try block completes abruptly for any other reason R, then the finally block is executed. Then there is a choice: 
     o If the finally block completes normally, then the try statement completes abruptly for reason R. 
     o If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded).