java在catch子句编译错误中检查了异常

时间:2010-05-02 04:33:11

标签: java exception-handling

我期待以下程序中的编译错误,因为catch块中的throw语句因为IOException是一个经过检查的异常而且它没有被catch块中的另一个try块捕获。但我得到了“华友世纪!”打印。任何解释都将非常感激。

根据JLS 11.2.3, http://java.sun.com/docs/books/jls/third_edition/html/exceptions.html

  

如果是方法,则是编译时错误   或者构造函数体可以抛出一些   异常类型E时两者都有   以下举行:

* E is a checked exception type
* E is not a subtype of some type declared in the throws clause of the
     

方法或构造函数。

import java.io.*;
public class Test{ 
    public static void main(String args[])
    {


        System.out.println(method()); 
    } 
    public static int method()
    {


        try{ 
            throw new Exception(); 
        }
        catch(Exception e){

            throw new IOException(); //No compile time error
        } 
        finally{

                    System.out.println("Hurray!");

        } 
    }
}

提前致谢。

2 个答案:

答案 0 :(得分:-1)

使用Eclipse的内置Java编译器会产生编译错误。

与JDK 6.0编译器相同。

您使用的Java版本没有编译错误?

答案 1 :(得分:-1)

也许我错过了什么,但你的程序中的throws clause在哪里?

现在为method()显示的源代码在方法头中不包含throws子句,只是throw语句。您从JLS引用的引用明确引用了throws子句。