Error Handling

You may have noticed something a little funny with the code fragments in this chapter: I haven't put any try-catch blocks around them. That's not an oversight. PrintStream methods never throw IOExceptions. Each method in the class catches IOException. When an exception occurs, an internal flag is set to TRue. You can test this flag using the checkError( ) method:

public boolean checkError( )

This method returns true if this print stream has ever encountered an error during its lifetime. Most of the time, you just ignore this, since print streams are only used in situations where exhaustive error checking is unnecessary.

There's also a protected setError( ) method you can use to signal an error from a subclass:

protected void setError( )

Once an error has been set, there's no way to unset it. Generally, once a PrintStream has encountered an error, all further writes to it silently fail. It's not the failure but the silence that makes PrintStream unsuitable for most applications.

Basic I/O

Introducing I/O

Output Streams

Input Streams

Data Sources

File Streams

Network Streams

Filter Streams

Filter Streams

Print Streams

Data Streams

Streams in Memory

Compressing Streams

JAR Archives

Cryptographic Streams

Object Serialization

New I/O



Nonblocking I/O

The File System

Working with Files

File Dialogs and Choosers


Character Sets and Unicode

Readers and Writers

Formatted I/O with java.text


The Java Communications API


The J2ME Generic Connection Framework


Character Sets

Java I/O
Java I/O
ISBN: 0596527500
EAN: 2147483647
Year: 2004
Pages: 244 © 2008-2020.
If you may any questions please contact us: