我正在尝试从class实现HighestScoreFile.java,并且在编译时出现此错误:

...MemoryGame.java:211: error: unreported exception IOException; must be caught or declared to be thrown
                    HighestScoreFile.HighestScoreFile(input, hours, minutes, seconds, click);
                                                     ^
1 error


在实现此HighestScoreFile.java之前,我已经使用main类进行了测试

public static void main(String[] args) throws IOException
    {
        HighestScoreFile("abcdefg", 12, 13, 14, 30);
    }


HighestScoreFile.java用于将数据保存到Highest.txt

但是,当我使用下面的代码实现另一个.java时,它将显示该错误。

HighestScoreFile.HighestScoreFile(input, hours, minutes, seconds, click);


我该如何解决这个问题?

最佳答案

您需要将异常抛出到方法之外:

public void someMethod() throws IOException
{
    // ...
    HighestScoreFile.HighestScoreFile(input, hours, minutes, seconds, click);
    // ..
}


或赶上兴奋:

try
{
    HighestScoreFile.HighestScoreFile(input, hours, minutes, seconds, click);
}
catch (IOException ex)
{
    // handle the exception
}


我建议您遵循Java exception trail

09-05 00:54