构造默认构造函数时无法处理exception:类型由隐式超级构造函数抛出的exception

代码工作正常,直到我尝试将代码变成可构造的类。 当我尝试从中构造一个对象时,我得到了错误

“默认构造函数无法处理由隐式超级构造函数抛出的exception类型IOException。必须定义一个显式构造函数”

这是必须向FileReaderBufferedReader抛出exception的时候。

谢谢

编辑:

 FileReader textFilethree = new FileReader (xFile); BufferedReader bufferedTextthree = new BufferedReader (textFilethree) ; String lineThree = bufferedTextthree.readLine(); 

xFile是从构造中获得的。 请注意,在此构造中抛出exception。

默认构造函数隐式调用超级构造函数,该构造函数假定抛出了一些需要在子类的构造函数中处理的exception。 详细解答后发布代码

 class Base{ public Base() throw SomeException{ //some code } } class Child extends Base{ public Child(){ //here it implicitly invokes `Base()`, So handle it here } } 

扩展类构造函数隐式调用基类super.constructor:

 class Base { public Base () throws Exception { throw <>; } } class Derived extends Base { public Derived () { } } 

现在,需要在Derived()处理exception或者将构造函数作为,

 public Derived() throws Exception { } 

无论你使用Derived的对象创建什么方法,要么将它包含在try-catch要么使该方法抛出Exception ,如上所述。 [注意:这是伪代码]

任何扩展超类的子类,其默认构造函数处理一些exception,子类必须有一个实现exception的默认构造函数

class Super {public Super()throws Exception {}}

class Sub extends Super {public Sub()throws Exception {//}}