2013-02-27 68 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

回答

相关问题