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!");
}
}
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources