构造默认构造函数时无法处理异常:隐式超级构造函数抛出的类型异常 [英] While constructing the default constructor can not handle exception : type Exception thrown by implicit super constructor
问题描述
代码工作正常,直到我尝试将代码变成可构造的类.当我尝试从中构造一个对象时,我收到错误
The code works fine until I try to make the code into a constructable class. When I attempt to construct an object from it I get the error
默认构造函数无法处理隐式超级构造函数抛出的异常类型 IOException.必须定义一个显式构造函数"
"Default constructor cannot handle exception type IOException thrown by implicit super constructor. Must define an explicit constructor"
这是必须向 FileReader
和 BufferedReader
抛出异常时.
This is when having to throw exceptions to FileReader
and BufferedReader
.
谢谢
FileReader textFilethree = new FileReader (xFile);
BufferedReader bufferedTextthree = new BufferedReader (textFilethree) ;
String lineThree = bufferedTextthree.readLine();
xFile 是从构造中获取的.请注意,在此构造中会抛出异常.
The xFile is gotten from the construction. Note that within this construction exceptions are thrown.
推荐答案
默认构造函数隐式调用超级构造函数,该构造函数被假定为抛出一些您需要在子类的构造函数中处理的异常.详细答案贴上代码
Default constructor implicitly invokes super constructor which is assumed to be throwing some exception which you need to handle in sub class's constructor . for detailed answer post the code
class Base{
public Base() throw SomeException{
//some code
}
}
class Child extends Base{
public Child(){
//here it implicitly invokes `Base()`, So handle it here
}
}
这篇关于构造默认构造函数时无法处理异常:隐式超级构造函数抛出的类型异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!