使用此代码:
public class DowncastTest {
public static void main(String[] args) {
try {
System.out.println(1);
} catch (Exception ex) {
Throwable cause = ex.getCause();
if (cause != null) {
Exception exCause = (Exception)cause;
System.out.println(exCause);
}
}
}
}
为什么 javac 不给出未经检查的强制转换警告?
Exception
extends Throwable
,因此您不能只将所有Throwable
s 转换为Exception
.