PrintStream setError() method in Java with Examples
Last Updated :
31 Jan, 2019
The setError() method of PrintStream Class in Java is used to set the error state of this PrintStream instance. This method is used as an indicator to indicate that an error has occurred in the stream. It is protected and hence needs to be implemented by deriving the PrintStream class to use it.
Syntax:
protected void setError()
Parameters: This method do not accepts any parameter.
Return Value: This method do not return anything.
Below methods illustrates the working of setError() method:
Program 1:
import java.io.*;
class GFG extends PrintStream {
public GFG(OutputStream out)
{
super (System.out);
}
public static void main(String[] args)
{
String str = "GeeksForGeeks" ;
try {
GFG stream
= new GFG(System.out);
stream.print(str);
stream.flush();
stream.setError();
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Program 2:
import java.io.*;
class GFG extends PrintStream {
public GFG(OutputStream out)
{
super (System.out);
}
public static void main(String[] args)
{
try {
GFG stream
= new GFG(System.out);
stream.write( 65 );
stream.flush();
stream.setError();
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Share your thoughts in the comments
Please Login to comment...