gpt4 book ai didi

java - 构造默认构造函数时无法处理异常: type Exception thrown by implicit super constructor

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:17:56 25 4
gpt4 key购买 nike

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

"Default constructor cannot handle exception type IOException thrown by implicit super constructor. Must define an explicit constructor"

这是当必须向 FileReaderBufferedReader 抛出异常时。

谢谢

编辑:

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

xFile 是从构建中获取的。请注意,在此构造中会抛出异常。

最佳答案

默认构造函数隐式调用 super 构造函数,它被假定为抛出一些您需要在子类的构造函数中处理的异常。有关详细答案,请发布代码

class Base{

public Base() throw SomeException{
//some code
}

}

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

关于java - 构造默认构造函数时无法处理异常: type Exception thrown by implicit super constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6772709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com