默认构造函数无法处理隐式超级构造函数抛出的异常类型ioexception

default constructor cannot handle exception type ioexception thrown by implicit super constructor

我已经在寻找类似的线索,但我找不到答案,因为这些帖子已经有 3 或 4 年的历史了,而且由于我的声誉不佳而不能问那里的人,我会再发一个帖子。

`

File file1 = new File("file1.txt");

File file2 = new File("file2.txt");

boolean isTwoEqual = FileUtils.contentEquals(file1,file2);

{ 

if (isTwoEqual == true)

System.out.println("You have no new grades");

else 
     System.out.println("You have new grade.");

}`

所以我需要检查两个 .txt 文件是否相等。 我收到一条错误消息 "default constructor cannot handle exception type ioexception thrown by implicit super constructor" 任何想法如何解决这一问题?

该错误意味着您正在进行此操作:

public class SomeParentClass {
    public SomeParentClass() throws IOException {
        // This is a parent class; maybe you wrote it, maybe you're using one.
        // note: It declares that it throws IOException!
    }
}

你正在写:

public class MyClass extends SomeParentClass {}

问题如下:您编写的任何class必须至少有一个构造函数。请注意,'MyClass' 具有零个定义的构造函数;当您这样做并尝试编译该文件时,javac 将为您创建一个。 Javac 是非常可预测的;它总是使这个构造函数:

public MyClass() {
    super();
}

这里也是如此。不幸的是,这是一个问题:super() 调用可能会抛出 IOException,您需要处理它。这个问题最简单的解决方案是编写自己的实际构造函数;不要依赖 javac 来为你做。所以,添加这个:

public MyClass() throws IOException {
    super();
}

编译器错误将消失。