java - FileNotFoundException未报告

标签 java compiler-errors filenotfoundexception

尝试用我的测试代码中的文件实例化对象时,我遇到未报告的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/

相关文章:

android - classes.dex不存在

java - maven-cobertura-plugin 不显示来源

java - 如何使用Maven处理跨平台(Windows + Linux)Java Native Interface项目?

java - 这段代码在 Java 中有效吗?

c# - FileNotFoundException 和文件路径

java - 我在尝试 httpcon.getInputStream() 时遇到 FileNotFoundException。有没有办法方便的检查是否是404网页?

java - 如何使用 hibernate 将这个复杂的对象持久保存到数据库中?

c++ - 为什么这个模板参数不可推导?

java - sbt 0.13.17 "Error compiling sbt component ' 编译器接口(interface)”

ios - Xcode 5和Cocos2d : Random Errors Like: 'No Known Class Method for Selector ' scene'?