我已经覆盖父方法并在该方法上添加了一个throws
声明。当我添加throws Exception
和throws FileNotFoundExceprion
时,它给了我错误,但与throws NullPointerException
一起工作。是什么原因?为什么NPE的工作原理,但不是例外和FileNotFoundException
class Vehicle {
public void disp() {
System.out.println("in Parent");
}
}
public class Bike extends Vehicle {
public void disp()throws NullPointerException {
System.out.println("in Child");
}
public static void main(String[] args) {
Vehicle v = new Bike();
v.disp();
}
}
因为NullPointerException扩展了RuntimeException并且这不会中断覆盖 – Silvinus
当您覆盖不声明它抛出它的方法时,不能抛出检查异常。 – khelwood
不知道你为什么被低估。对于不了解Java中已检查和未检查异常的细节的人来说,这可能会让人感到困惑。而且我不知道在这种情况下我会弄清楚Google要做什么。 – sstan