code
16px-Pencil

Standard error is an standard output stream where a program may write its error messages.

The following snippets show how to do this using various languages.

Snippets

C

#include <stdio.h>

fprintf(stderr, "something broke :-(\n");

C++

#include <iostream>

std::cerr << "something broke :-(" << std::endl;

C#

System.Console.Error.WriteLine("something broke :-(");

Java

System.err.println("something broke :-(");

OCaml

prerr_endline "something broke :-(";
Printf.eprintf "something broke :-(\n";

Perl

print STDERR "something broke :-(\n";

PHP

When using PHP in CLI mode (Command Line Interface), you can send error messages to stderr:

fputs(STDERR, 'Error!'); // sends 'Error!' to stderr

Under a webserver, the STDERR stream is usually linked to the web server's error log.

Python

2.x

import sys
print >> sys.stderr, "something broke :-("
sys.stderr.write("something broke :-(") # no trailing newline

3.x

import sys
print("something broke :-(", file=sys.stderr)

Ruby

To just write to standard error:

$stderr.puts("Error goes here")

If you want to create an IO object and point that to standard error:

# the file descriptor 2 = standard error
errors = IO.new(2, "w")
errors.puts("Error goes here")

Tcl

puts stderr "An error message"

External links

See also