The Ubiquitous IOException

As computer operations go, input and output are unreliable. They are subject to problems completely outside the programmer’s control. Disks can develop bad sectors while a file is being read; construction workers drop backhoes through the cables that connect your WAN; users unexpectedly cancel their input; telephone repair crews shut off your modem line while trying to repair someone else’s. (This last one actually happened to me while writing this chapter. My modem kept dropping the connection and then not getting a dial tone; I had to hunt down the telephone “repairman” in my building’s basement and explain to him that he was working on the wrong line.)

Because of these potential problems and many more, almost every method that performs input or output is declared to throw IOException. IOException is a checked exception, so you must either declare that your methods throw it or enclose the call that can throw it in a try/catch block. The only real exceptions to this rule are the PrintStream and PrintWriter classes. Because it would be inconvenient to wrap a try/catch block around each call to System.out.println(), Sun decided to have PrintStream (and later PrintWriter) catch and eat any exceptions thrown inside a print() or println() method. If you do want to check for exceptions inside a print() or println() method, you can call checkError() :

public boolean checkError()

The checkError() method returns true if an exception has occurred on this print stream, false if one hasn’t. It only tells you that an error occurred. It does not tell you what sort of error occurred. If you need to know more about the error, you’ll have to use a different output stream or writer class.

IOException has many subclasses—15 in java.io—and methods often throw a more specific exception that subclasses IOException. (However, methods usually only declare that they throw an IOException.) Here are the subclasses of IOException that you’ll find in java.io:

CharConversionException

EOFException

FileNotFoundException

InterruptedIOException

InvalidClassException

InvalidObjectException

NotActiveException

NotSerializableException

ObjectStreamException

OptionalDataException

StreamCorruptedException

SyncFailedException

UTFDataFormatException

UnsupportedEncodingException

WriteAbortedException

 

There are a number of IOException subclasses scattered around the other packages, particularly java.util.zip (DataFormatException and ZipException) and java.net (BindException, ConnectException, MalformedURLException, NoRouteToHostException, ProtocolException, SocketException, UnknownHostException, and UnknownServiceException).

The java.io.IOException class declares no public methods or fields of significance—just the usual two constructors you find in most exception classes:

public IOException()
public IOException(String message)

The first constructor creates an IOException with an empty message. The second provides more details about what went wrong. Of course, IOException has the usual methods inherited by all exception classes such as toString() and printStackTrace().

Get Java I/O now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.