簡體   English   中英

Java中的異常處理

[英]Exception handling in 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塊中有一個“拋出”並最終在catch塊之后的塊的示例(如上面的示例)。 任何人都可以闡明它嗎?

謝謝。

引用Java語言規范§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). 

您將看到第一個。 最終塊將始終執行並最后執行。

這是流行測驗嗎? 開玩笑。 如果有例外,您將看到第一個。 finally塊語句將始終被執行,因此將始終打印該語句。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM