views:

757

answers:

6

After providing some answers here, and reading some comments, it would seem that, in practice IOException is never thrown on close for file I/O.

Are there any cases in which calling close on a Stream/Reader/Writer actually throws an IOException?

If an exception is actually thrown, how should it be dealt with?

+5  A: 

For files, you may not see IOException thrown often on close(), but you'll definitely see it for non-File I/O like closing sockets to the network.

Here's an example of a Java bug where closing a UDP socket eventually caused an IOException to be thrown.

Martin
yup, anything that can "go away" can cause that to hapen. A hard drive crash could probably do it too.
TofuBeer
+5  A: 

I have found two cases:

  • Losing the network connection when there is still data in the buffer to be flushed.
  • Having the file system fill up (or reaching your user limit for file size) when there is still data in the buffer to be flushed.

Both of those examples depend on something happening while there is still data in the buffer. Close flushes the buffer before the file is closes, so if there is an error writing the data to the file it throws an IOException.

If you execute the following code passing it the name of a file to create on a network drive, and then before you press the enter key unplug your network cable, it will cause the program to throw an IOException in close.

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class Test
{
    public static void main(final String[] argv)
    {
        final File file;

        file = new File(argv[0]);
        process(file);
    }

    private static void process(final File file)
    {
        Writer writer;

        writer = null;

        try
        {
            writer = new FileWriter(file);
            writer.write('a');
        }
        catch(final IOException ex)
        {
            System.err.println("error opening file: " + file.getAbsolutePath());
        }
        finally
        {
            if(writer != null)
            {
                try
                {
                    try
                    {
                        System.out.println("Please press enter");
                        System.in.read();
                    }
                    catch(IOException ex)
                    {
                        System.err.println("error reading from the keyboard");
                    }

                    writer.close();
                }
                catch(final IOException ex)
                {
                    System.err.println("See it can be thrown!");
                }
            }
        }
    }
}
TofuBeer
Have fun with that badge that you're about to get ;-)
Joachim Sauer
lol - thanks. Had to go look up what it was :-)
TofuBeer
+4  A: 
erickson
Wish I could accept two answers.
TofuBeer
+2  A: 

An examination of what can happen when calling close, how exception hiding can affect you and what you can do about it: blog post.

McDowell
+2  A: 

It's specifically FileInputStream.close which does not throw, even if your hard drive is on fire. Presumably it is the same for socket input. For output streams you may also be flushing. until relatively recently BufferedOutputStream used to fail to close the underlying stream if flush threw.

Tom Hawtin - tackline
A: 

I've also noticed that PrintWriter doesn't even have a throws clause....

Andrei Vajna II
However, it does have a way to test if an error has occurred.
Stephen C