尝试用我的测试代码中的文件实例化对象时,我遇到未报告的FileNotFoundException错误。我正在使用/创建的类在构造函数(只有一个构造函数)中具有FileNotFoundException,因此我不太确定为什么在声明对象时为什么会要求我提供额外的FileNotFound。
//Constructor
public readFile(File file)throws FileNotFoundException {
//do i need to create a file object here?
Scanner inScanFile = new Scanner(file);
}
///////////Running Code from JUNIT below//////////////
public void Empty(){
File testFile = new File("HARRY_POTTER_TRIVIA.txt");
ReadingClass newReadtest = new ReadingClass(testFile); //Error occurs here
最佳答案
public readFile(File file)throws FileNotFoundException {
//do i need to create a file object here?
没有为什么?您已经有一个。
Scanner inScanFile = new Scanner(file);
这可能会抛出
FileNotFoundException
,这就是为什么此构造函数必须捕获它或声明将其或其基类之一抛出的原因。 }
///////////Running Code from JUNIT below//////////////
public void Empty(){
File testFile = new File("HARRY_POTTER_TRIVIA.txt");
ReadingClass newReadtest = new ReadingClass(testFile); //Error occurs here
那是因为
readFile()
可以抛出FileNotFoundException
,因此,再次,您必须捕获它或声明您将其抛出,或者它的基类之一。
关于java - FileNotFoundException未报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45527570/