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?
in.read(c)orout.write(c).