我在某处读取了此代码,当我在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 {
}
谁能告诉我为什么它显示此警告。
我要感谢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 {
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句