Unhandled Exception Type IOException [duplicate]

我的梦境 提交于 2020-01-30 08:09:05

问题


Possible Duplicate:
Why do I get the “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(); //###
}

回答1:


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?

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.




回答2:


One solution: change to

private static void euler8() throws IOException {

But then the calling method has to catch the IOException.

or catch the Exception:

private static void euler8()
{   
    int c =0;
    int b;
    ArrayList<Integer> bar = new ArrayList<Integer>(0);
    BufferedReader reader;
    try { 
        File inFile = new File("euler8.txt");
        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*/
        }
    } catch (IOException ex) {
       // LOG or output exception
       System.out.println(ex);
    } finally {
        try {
           reader.close(); //###
        } catch (IOException ignored) {}
    }
}



回答3:


Wrap in a try/catch block to catch the Exceptions.

If you do not do that it will go unhandled.




回答4:


What happens if you can't read the nominated file ? The FileInputStream will throw an exception and Java mandates that you'll have to check for this and handle it.

This type of exception is called a checked exception. Unchecked exceptions exist and Java doesn't require you to handle these (largely because they're unhandable - e.g. OutOfMemoryException)

Note that your handling may include catching it and ignoring it. This isn't a good idea, but Java can't really determine that :-)



来源:https://stackoverflow.com/questions/13957961/unhandled-exception-type-ioexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!