2013-02-27 51 views
0

有人可以解釋爲什麼第一個演員不給CCE?Java異常。將其中一個類型投射到另一個類型

public class Test { 

    public static void main(String[] args) throws Throwable { 
    Test.<RuntimeException>throwIt(new Exception()); 
    } 

    @SuppressWarnings("unchecked") 
    private static <T extends Throwable> void throwIt(Throwable throwable) throws T { 
    throw (T) throwable; // no ClassCastException 
    throw (RuntimeException) throwable; // ClassCastException(as it should be) 
    } 
} 

P.S.評論一個演員(否則它不會編譯)。

+1

第二'throw'不可達 – Reimeus 2013-02-27 11:38:27

+0

什麼是你的問題?你的代碼是否編譯?您將收到無法訪問的代碼編譯錯誤。 – Jayamohan 2013-02-27 11:38:45

+0

哦,常見,然後評論其他 – Bax 2013-02-27 11:39:02

回答

相關問題