Java.io.PrintStream.checkError() Method



Description

The java.io.PrintStream.checkError() method flushes the stream and checks its error state. The internal error state is set to true when the underlying output stream throws an IOException other than InterruptedIOException, and when the setError method is invoked. If an operation on the underlying output stream throws an InterruptedIOException, then the PrintStream converts the exception back into an interrupt by doing −

Declaration

Following is the declaration for java.io.PrintStream.checkError() method.

public boolean checkError()

Parameters

NA

Return Value

This method returns true if and only if this stream has encountered an IOException other than InterruptedIOException, or the setError method has been invoked.

Exception

NA

Example

The following example shows the usage of java.io.PrintStream.checkError() method.

package com.tutorialspoint;

import java.io.*;

public class PrintStreamDemo {
   public static void main(String[] args) {
      String s = "Hello World.";

      // create a new PrintStream
      PrintStream ps = new PrintStream(System.out);

      // print a string
      ps.println(s);

      // check for errors and print
      ps.print(ps.checkError());
      ps.flush();
      ps.close();
   }
}

Let us compile and run the above program, this will produce the following result −

Hello World.
false
java_io_printstream.htm
Advertisements