What is the System.setErr() function in Java?

Overview

The staticmethod accessible with class name System.setErr() function is used to reassign the standard error output stream.

Syntax


static void setErr(PrintStream error)

Parameters

  • error: the new standard error output stream.

Return value

This function does not return a value.

SecurityException: In this exception, the checkPermission() method does not allow the user to reassign the latest standard error output stream when the security manager exists.

Example

The code below demonstrates how the setErr() function works in Java.

// Load java input output package
import java.io.*;
// Main class
public class EdPresso {
public static void main(String[] args) throws FileNotFoundException {
OutputStream outst = new FileOutputStream("file1.txt");
PrintStream _printStream = new PrintStream(outst);
// set current standarad error output stream
System.setErr(_printStream);
System.out.println("Task completed!");
}
}

Free Resources