如何把'抛出IOException`放到语句中 [英] How to put `throws IOException` to the statement

查看:502
本文介绍了如何把'抛出IOException`放到语句中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 HighestScoreFile.java 实现,当我编译时,我收到此错误:

I am trying to implement a class from HighestScoreFile.java and when I compile, I get this error :

...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 ,我已经使用

Before I implement this HighestScoreFile.java, I have tested with a main class using

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

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

但当我实施到另一个<$ c时$ c> .java 使用下面的代码显示该错误。

But when I implement to another .java using the code below, it shows out that error.

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

如何解决此问题?

推荐答案

你需要在方法之外抛出异常:

You need to either throw the exception outside of the method:

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例外小道

这篇关于如何把'抛出IOException`放到语句中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆