1

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(); //###
}
Community
  • 1
  • 1
Bennett
  • 317
  • 2
  • 6
  • 16
  • 3
    Read the [Exceptions Tutorial](http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html) and then use that knowledge to either wrap the code in a try/catch or throw the exception -- your choice. – Hovercraft Full Of Eels Dec 19 '12 at 17:36

4 Answers4

6

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I added throws to the method signature, but how would I include the finally block if I don't include try{}? – Bennett Dec 19 '12 at 17:40
  • @JamesRoberts - You can use a `try/finally` structure (without any `catch` blocks). An alternative, if you're using Java 7, is to use a [try-with-resources](http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) statement. – Ted Hopp Dec 19 '12 at 17:41
  • Also I am no longer getting errors within the method, however there is a `Unhandled Exception Type IOException` error on the method call. – Bennett Dec 19 '12 at 17:41
  • 1
    @JamesRoberts - At some level in the call hierarchy you will need to deal with the exceptions that might arise from your code. It all goes back to Jon's question: what do you want to happen if there is an exception (for instance, if the file cannot be opened)? – Ted Hopp Dec 19 '12 at 17:43
  • @James: please read the tutorial as it will explain all and is better than trying to use tools without trying to first understand them. – Hovercraft Full Of Eels Dec 19 '12 at 17:47
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) {}
    }
}
AlexWien
  • 28,470
  • 6
  • 53
  • 83
0

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

If you do not do that it will go unhandled.

Naftali
  • 144,921
  • 39
  • 244
  • 303
0

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 :-)

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440