以下代码完美编译。我相信这是因为编译器在编译时知道控件将转到 finally 块并抛出未经检查的异常(这没关系,不需要处理)并且它知道代码之前抛出的所有其他异常至此都迷失了。所以不用担心他们。
try{
// DoSomething();
}catch(Exception e){
// Throw checked exception
}finally{
// Throw unchecked exception
}
例子:
public class TestClass {
public static void main(String[] args) {
TestClass c = new TestClass();
try {
// Whatever
} catch (Exception e) {
throw new FileNotFoundException();
} finally {
throw new NullPointerException();
}
}
}
到目前为止一切顺利,直到我从方法中抛出未经检查的异常。
try{
// DoSomething();
}catch(Exception e){
// Call a method that throws a checked exception
// or just throw the checked exception from here
}Finally{
// Call a method that throw an unchecked exception
}
例子:
public class TestClass {
public static void main(String[] args) {
TestClass c = new TestClass();
try {
//Whatever
} catch (Exception e) {
c.m1();
// or just throw it here
// throw new FileNotFoundException();
} finally {
c.m2();
}
}
public void m1() throws IOException {
throw new FileNotFoundException();
}
public void m2() throws RuntimeException {
throw new NullPointerException();
}
}
此代码不会编译。它将 c.m1() 标记为错误“未处理的异常类型 _”(eclipse)或“未报告的异常 _;必须被捕获或声明为抛出”(cmd)。
就像它忽略了finally块将抛出 LAST 异常(未检查)并且无需担心 catch 块中的异常,即使它是未处理的检查异常,因为它们无论如何都会丢失!知道 m2() 被声明为专门抛出未经检查的异常 (RuntimeException)。
有人对为什么第二个代码中存在编译错误有更好的解释吗?谢谢 :)