由於catch語句中的throw語句,我期待在下面的程序中出現編譯錯誤,因爲IOException是一個檢查異常,並且它不會被catch塊中的另一個try塊捕獲。但我得到了「歡呼!」打印。任何解釋將不勝感激。java中檢查異常的catch子句編譯錯誤
根據JLS 11.2.3, http://java.sun.com/docs/books/jls/third_edition/html/exceptions.html
這是一個編譯時錯誤,如果一個方法 或構造體可以拋出一些 異常E型當兩個 的下列保持:
* E is a checked exception type * E is not a subtype of some type declared in the throws clause of the
方法或構造函數。
import java.io.*;
public class Test{
public static void main(String args[])
{
System.out.println(method());
}
public static int method()
{
try{
throw new Exception();
}
catch(Exception e){
throw new IOException(); //No compile time error
}
finally{
System.out.println("Hurray!");
}
}
}
在此先感謝。
奇怪!我也在使用eclipse galileo。這裏沒有編譯錯誤。 – srandpersonia 2010-05-02 04:49:29
我正在使用jdk 6更新14. – srandpersonia 2010-05-02 04:52:10
唷!在創建一個新的類文件並複製粘貼後立即獲得它。我認爲它應該是因爲我在使用Exception之前將其更改爲IOException並且沒有發生自動構建。感謝您的回覆和時間。 – srandpersonia 2010-05-02 04:56:32