VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/console-seterror-method-in-c-sharp/

⇱ Console.SetError() Method in C# - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Console.SetError() Method in C#

Last Updated : 11 Jul, 2025
The Console.SetError(TextWriter) Method sets the Error property of the specified StreamWriter i.e., it redirects the standard error stream to a file. As the console is set with this StreamWriter object, the WriteLine() method can be called to write the error into the file.
Syntax: public static void SetError (System.IO.StreamWriter newError); Parameter: newError: It is a stream which is the new standard error output.
Exception: This method will throw the ArgumentNullException if the parameter passed is null. Also, since it uses the StreamWriter object, it's exceptions should also be taken care of. Example: In this example, the SetError() method is used to set the StreamWriter object to the console, and the error messages will be written into a log file from the Console. Output: The gfg_error.log file will now have the error messages in it. 👁 Image
Reference:
Comment
Article Tags:

Explore