投掷的实例没有抛出

时间:2017-02-07 12:07:34

标签: java exception exception-handling

我在某处读取了这段代码,当我在netbeans中编写它时,它正在打印所需的输出,但会显示一条警告" Throwable实例未被抛出"

class Demo {

    public static void f1() throws MyException {
        throw new MyException; // Warning at this line i.e. Throwable instance not thrown
    }

    public static void f2() throws MyException {
        f1();
    }

    public static void f3() throws MyException {
        f2();
    }

    public static void main(String[] args) {
        try {
            f3();
        } catch (Exception e) {
            System.out.println("Exception in handled in main method");
        }
    }
}

class MyException extends Exception {

}

任何人都可以告诉我为什么它会显示此警告。

1 个答案:

答案 0 :(得分:0)

我想感谢QBrute指出错误。

更正代码

class Demo {

    public static void f1() throws MyException {
        throw new MyException(); // I forgot to add parenthesis.
    }

    public static void f2() throws MyException {
        f1();
    }

    public static void f3() throws MyException {
        f2();
    }

    public static void main(String[] args) {
        try {
            f3();
        } catch (Exception e) {
            System.out.println("Exception in handled in main method");
        }
    }
}

class MyException extends Exception {

}