2

The following program for file I/O is taken from the standard Oracle docs:

//Copy xanadu.txt byte by byte into another file import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopyBytes { public static void main(String[] args) //throws IOException { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("xanadu.txt"); out = new FileOutputStream("xanadu_copy.txt"); int c; while((c = in.read()) != -1) { out.write(c); } } catch (IOException e) { System.out.println("IO exception : " + e.getMessage()); } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } } 

As you can see, I commented out the throws IOException part, thinking that since I'm catching it in the code, all should be fine. But I'm getting a compiler error:

CopyBytes.java:32: error: unreported exception IOException; must be caught or declared to be thrown in.close(); ^ CopyBytes.java:36: error: unreported exception IOException; must be caught or declared to be thrown out.close(); ^ 2 errors 

The errors vanish when I include the throws IOException part, but I'm confused. Isn't it enough that I'm catching the exceptions?

4
  • Did you look at where the compiler says the error is? Hint: it's not in.read(c) or out.write(c). Commented May 1, 2015 at 9:52
  • @immibis I got it now, after reading the answers below. Foolish me. :P Commented May 1, 2015 at 9:54
  • I mean where... as in, the filename and line number, right at the start of each error. Commented May 1, 2015 at 9:56
  • @immibis Yes, I got it now. Wasn't thinking straight, I guess. :D Commented May 1, 2015 at 9:56

3 Answers 3

6

You are not catching the potential IOException that might be thrown in your finally block.

You can fix it by adding try-catch to the finally block:

 finally { try { if (in != null) { in.close(); } if (out != null) { out.close(); } } catch (IOException ex) { } } 
Sign up to request clarification or add additional context in comments.

3 Comments

catch(IOException ex) {} is a bad idea - if an exception happens, the program won't tell you that something went wrong. Put ex.printStackTrace() at the very least.
@immibis There's not much you can do about this exception when closing the streams. What would you do if you see this stack trace in some log file?
then the stack trace would tell you why the stream failed to close. (Full hard disk and stuff buffered, for example)
4

In your finally block you are using in.close() statement to close the stream. This statement also throws IOException. You can avoid this exception by adding try/catch block to these close statements.

Comments

3

In order to not have to report an exception you need to put it in the try portion of the try/catch/finally block. Exceptions thrown in the catch and finally parts are not caught, prompting the compiler error:

try { ... // Exceptions thrown from here will be caught } catch (ExceptionOne e1) { .. // Exceptions thrown from here need to be declared } catch (ExceptionTwo e2) { .. // Exceptions thrown from here need to be declared } finally { .. // Exceptions thrown from here need to be declared } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.