两个类,关系如下
public class Parent {
public Parent() throws RemoteException {
System.out.println("in parent...");
}
}
public class Child extends Parent {
    //error
    //Default constructor cannot handle exception type RemoteException thrown by implicit super 
    //constructor. Must define an explicit constructor
}为什么当parent的构造器抛出RemoteException的时候,Child会编译错误?如果Parent的构造器抛出的是NullpointException,为什么上述代码又是没有问题的呢?

解决方案 »

  1.   

    NullPointerException继承自java.lang.RuntimeException,运行时异常不必显式的捕获.
    这里估计RemoteException需要捕获吧.
      

  2.   

    在子类的构造方法也写上throws 异常就可以了.
      

  3.   

    RemoteException是IOException子类,他属于非运行是异常,你throws的同时需要try-catch进行捕获异常。
    NullPointerException就如1楼所说的,它是继承自RuntimeException,运行时异常不必显式的捕获. 
      

  4.   

    非常感谢大家的回复,可我还是有个疑问,为什么父类的构造器抛出RemoteException的时候,子类也必须要有一个构造器抛出此异常呢?java是怎样一种类加载机制?我查了些资料好像没有直接说明这个问题的