When might an IOError be thrown?

Here is an explanation from Mark Reinhold from Oracle:

The new IOError class was defined in combination with the new java.io.Console class. It’s for use in situations where an unrecoverable I/O error occurs and the most appropriate response is to terminate the program rather than attempt to handle the exception.

The IOError class, along with many other enhancements, will be documented in the forthcoming Mustang maintenance review in the JCP.

http://cafe.elharo.com/blogroll/undocumented-changes-in-java-6-mustang-ioerror/


Is there ever a case when IOError would be thrown in java?

import java.io.IOError;

public class Test {

    public static void main(String[] args) {
        throw new IOError(new Exception());
    }

}

will result in

Exception in thread "main" java.io.IOError: java.lang.Exception
    at test.Test.main(Test.java:13)
Caused by: java.lang.Exception
    ... 1 more
Java Result: 1

I believe you expect a case which is more likely to happen.

An IOError would be thrown for example when trying to read from a console where the input stream has been closed.

You can try to run this snippet

import java.io.*;

public class Test {

    public static void main(String[] s) {
        Console con = System.console();
        try {
            InputStreamReader reader = new InputStreamReader(System.in);
            reader.close();
            String st = con.readLine("%s", "Enter a line");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IOError error) {
            error.printStackTrace();
        }
    }
}

That would result in

java.io.IOError: java.io.IOException: Stream Closed
    at java.io.Console.readLine(Console.java:254)
    at Test.main(Test.java:10)
Caused by: java.io.IOException: Stream Closed
    at java.io.FileInputStream.readBytes(Native Method)
    at java.io.FileInputStream.read(FileInputStream.java:246)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
    at java.io.Console$LineReader.read(Console.java:437)
    at java.io.Console.readline(Console.java:376)
    at java.io.Console.readLine(Console.java:250)
    ... 1 more

Console, Path#toAbsolutePath, and Path#toUri declare this particular exception to be thrown. Of course, that's a documentation fact and not an actual declaration; since Error is a runtime exception, declaring it to be thrown in the signature would have no meaning.

From what it looks like in code, Console#readLine and Console#readPassword catch an IOException that results through its normal operation, then propagate that to an IOError.

Essentially, IOError represents a critical failing of the underlying filesystem, or accessing some resource that ties Java to the file system. It's not thrown often, but it has the potential to be thrown if something serious happens from within the file system.

Tags:

Java

Ioerror