未处理的异常类型IOException [英] Unhandled Exception Type IOException

查看:88
本文介绍了未处理的异常类型IOException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
为什么获得未处理的异常类型IOException"?

我正在尝试使用以下算法来求解Euler#8.问题是,每当我修改有巨大注释的行时,在我用注释//### 标记的每一行上都会出现错误 Unhandled Exception Type IOException 代码>.

I'm trying to solve Euler #8 using the following algorithm. Problem is, whenver I modify the line I have the giant comment on, the error Unhandled Exception Type IOException appears on each line that I've marked with the comment //###.

private static void euler8()
{   
    int c =0;
    int b;
    ArrayList<Integer> bar = new ArrayList<Integer>(0);
    File infile = new File("euler8.txt");
    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(
                            new FileInputStream(infile), //###
                            Charset.forName("UTF-8")));
        while((c = reader.read()) != -1) { //###
          char character = (char) c;
          b = (int)character;
          bar.add(b); /*When I add this line*/
        }
    reader.close(); //###
}

推荐答案

是的, IOException 是经过检查的异常,这意味着您要么需要捕获它,要么声明您的方法也将抛出该错误.如果抛出异常,您想要会发生什么?

Yes, IOException is a checked exception, which means you either need to catch it, or declare that your method will throw it too. What do you want to happen if the exception is thrown?

请注意,无论如何,您通常应该始终在 finally 块中关闭 reader ,这样即使遇到另一个异常,它也会被关闭.

Note that you should generally be closing the reader in a finally block anyway, so that it gets closed even in the face of another exception.

请参阅关于异常的Java教程课程,以了解有关选中和删除的更多信息.未经检查的异常.

See the Java Tutorial lesson on exceptions for more details about checked and unchecked exceptions.

这篇关于未处理的异常类型IOException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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