1

以下代码完美编译。我相信这是因为编译器在编译时知道控件将转到 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)。

有人对为什么第二个代码中存在编译错误有更好的解释吗?谢谢 :)

4

1 回答 1

1

例外只是不“抛出”自己。您必须明确处理被调用方法抛出的任何检查异常——无论它们发生在哪里。你调用的任何抛出检查异常的东西都必须被 try catch 块包围,或者调用子方法的方法必须声明抛出相同类型的异常。这不适用于检查的异常。

这意味着,你有

catch (Exception e) {
     c.m1(); 
         // or just throw it here
         // throw new FileNotFoundException();
}

您必须捕获由抛出的已检查异常,m1因为main没有声明它抛出任何东西。

使用finallytry/catch 上的块,您仍然必须处理被调用方法抛出的任何已检查异常——即使在 catch 块中也是如此。但是,在您使用明确抛出运行时异常的 finally 块显式抛出已检查异常的情况下,编译器允许这样做,因为它可以确定事件的顺序是什么——在任何情况下都可以。

于 2013-12-05T20:22:41.987 回答