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

查看:45
本文介绍了未处理的异常类型 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.

有关已检查和未经检查的异常.

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

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

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