Hello world/Standard error: Difference between revisions

Content added Content deleted
(jq)
m ({{out}} / -Category:Scala Implementations / moved Category to top)
Line 1: Line 1:
{{task|Basic language learning}} {{selection|Short Circuit|Console Program Basics}}
{{task|Basic language learning}}{{selection|Short Circuit|Console Program Basics}}A common practice in computing is to send error messages to a different output stream than [[User Output - text|normal text console messages]]. The normal messages print to what is called "standard output" or "standard out". The error messages print to "standard error". This separation can be used to redirect error messages to a different place than normal messages.
[[Category:Streams]]
{{omit from|Applesoft BASIC}}
{{omit from|bc|Always prints to standard output.}}
{{omit from|Brainf***}}
{{omit from|dc|Always prints to standard output.}}
{{omit from|GUISS|Cannot customize error messages}}
{{omit from|Integer BASIC}}
{{omit from|TI-83 BASIC|Same reason as TI-89.}}
{{omit from|TI-89 BASIC|no analogue to stderr, unless you count graph display vs. program IO}}
{{omit from|Unlambda|No concept of standard error (or alternate output streams in general).}}

A common practice in computing is to send error messages
to a different output stream than [[User Output - text|normal text console messages]].

The normal messages print to what is called "standard output" or "standard out".
The error messages print to "standard error".

This separation can be used to redirect error messages to a different place than normal messages.


Show how to print a message to standard error by printing "Goodbye, World!" on that stream.
Show how to print a message to standard error by printing "Goodbye, World!" on that stream.
Line 29: Line 47:
put(stand error, ("Goodbye, World!", new line))
put(stand error, ("Goodbye, World!", new line))
)</lang>
)</lang>
{{out}}
Output:
<pre>
<pre>
Goodbye, World!
Goodbye, World!
Line 62: Line 80:


=={{header|AWK}}==
=={{header|AWK}}==
To print a message to standard error,
Pipe through a shell command:
pipe it through a shell command:


<lang awk>BEGIN {
<lang awk>BEGIN {
Line 77: Line 96:
}</lang>
}</lang>


With ''gawk'', ''mawk'' and ''nawk'': a special feature associates "/dev/stderr" with standard error. The manuals of ''gawk'' and ''mawk'' describe this feature; ''nawk'' also has this feature.
With ''gawk'', ''mawk'' and ''nawk'': a special feature
associates "/dev/stderr" with standard error.
The manuals of ''gawk'' and ''mawk'' describe this feature;
''nawk'' also has this feature.


Other implementations might try to open /dev/stderr as a file.
Other implementations might try to open /dev/stderr as a file. Some Unix clones, like [[BSD]], have a /dev/stderr device node that duplicates standard error, so this code would still work. Some systems have no such device node, so this code would fail. We recommend "cat 1>&2", which is more portable, and works with any Unix clone.
Some Unix clones, like [[BSD]], have a /dev/stderr device node
that duplicates standard error, so this code would still work.
Some systems have no such device node, so this code would fail.
We recommend "cat 1>&2", which is more portable, and works with any Unix clone.


=={{header|Batch File}}==
=={{header|Batch File}}==
<lang dos>1>&2 echo Goodbye, World!</lang>
<lang dos>1>&2 echo Goodbye, World!</lang>
The redirection operator <code>1>&2</code> causes all output on stream 1 (standard out) to be redirected to stream 2 (standard error). The redirection can be moved to the end of the line, too.
The redirection operator <code>1>&2</code> causes all output on stream 1 (standard out) to be redirected to stream 2 (standard error).
The redirection can be moved to the end of the line, too.


=={{header|BASIC}}==
=={{header|BASIC}}==
Line 115: Line 142:
return 0;
return 0;
}</lang>
}</lang>

=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>static class StdErr
<lang csharp>static class StdErr
Line 225: Line 253:


=={{header|Factor}}==
=={{header|Factor}}==
Start Factor in a terminal for this.
Start Factor in a terminal for this:
<lang factor>error-stream get [ "Goodbye, World! bbl, crashing" print flush ] with-output-stream*</lang>
<lang factor>error-stream get [ "Goodbye, World! bbl, crashing" print flush ] with-output-stream*</lang>


Line 249: Line 277:
=={{header|Fortran}}==
=={{header|Fortran}}==


Normally standard error is associated with the unit 0 but this could be different for different vendors. Therefore since Fortran 2003 there's an intrinsic module which defines the parameter ERROR_UNIT.
Normally standard error is associated with the unit 0
but this could be different for different vendors.
Therefore since Fortran 2003 there's an intrinsic module
which defines the parameter ERROR_UNIT.


<lang fortran>program StdErr
<lang fortran>program StdErr
Line 318: Line 349:
<lang jq>error("Goodbye, World!")</lang>
<lang jq>error("Goodbye, World!")</lang>


Note that although this satisfies the task requirement, error/1 also raises an error condition.
Note that although this satisfies the task requirement,
error/1 also raises an error condition.

=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>println(STDERR, "Goodbye, World!")</lang>
<lang julia>println(STDERR, "Goodbye, World!")</lang>
Line 425: Line 458:
END HelloErr.
END HelloErr.
</lang>
</lang>
{{out}}
Output:
<pre>
<pre>
Goodbye, World!
Goodbye, World!
</pre>
</pre>

=={{header|Objective-C}}==
=={{header|Objective-C}}==


Line 434: Line 468:
{{Works with|Cocoa}}
{{Works with|Cocoa}}


In Objective-C one can use the standard C library and the stderr as in the C language; nonetheless a common way to output to stderr for logging purpose and/or error notification is the <tt>NSLog</tt> function, that works almost like <tt>fprintf(stderr, "...")</tt>, save for the fact that the format string is an NSString object, and it also prepends a timestamp.
In Objective-C one can use the standard C library and the stderr as in the C language; nonetheless a common way to output to stderr for logging purpose and/or error notification is the <tt>NSLog</tt> function, that works almost like <tt>fprintf(stderr, "...")</tt>, save for the fact that the format string
is an NSString object, and it also prepends a timestamp.


<lang objc>#import <Foundation/Foundation.h>
<lang objc>#import <Foundation/Foundation.h>
Line 466: Line 501:
ooRexx provides a .error object that writes output to the standard error stream.
ooRexx provides a .error object that writes output to the standard error stream.
<lang ooRexx>.error~lineout("Goodbye, World!")</lang>
<lang ooRexx>.error~lineout("Goodbye, World!")</lang>
The .error object is a proxy that delegates to a backing stream, so this might be redirected. By default, this delegates to the .stderr object, which can also be used directly.
The .error object is a proxy that delegates to a backing stream, so this might be redirected.
By default, this delegates to the .stderr object, which can also be used directly.
<lang ooRexx>.stderr~lineout("Goodbye, World!")</lang>
<lang ooRexx>.stderr~lineout("Goodbye, World!")</lang>
or in 'Classic REXX style'
or in 'Classic REXX style'
Line 531: Line 567:


=={{header|PowerShell}}==
=={{header|PowerShell}}==
Since PowerShell has a slightly different system of pipes and streams (to facilitate easy usage from a host application) the standard Write-Error cmdlet is mainly for sending annotated error messages to the host:
Since PowerShell has a slightly different system of pipes and streams
(to facilitate easy usage from a host application) the
standard Write-Error cmdlet is mainly for sending annotated error messages
to the host:
<lang powershell>Write-Error "Goodbye, World!"</lang>
<lang powershell>Write-Error "Goodbye, World!"</lang>
Note that this outputs more than just the message because behind the scenes it is an uncaught exception:
Note that this outputs more than just the message,
because behind the scenes it is an uncaught exception:
<pre>Write-Error "Goodbye, World!" : Goodbye, World!
<pre>Write-Error "Goodbye, World!" : Goodbye, World!
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
Line 578: Line 618:
=={{header|REXX}}==
=={{header|REXX}}==
===version 1===
===version 1===
This version will work with any REXX under operating systems (hosts) that don't have (or support) either stream output,
This version will work with any REXX under operating systems (hosts)
that don't have (or support) either stream output,
<br>or don't have a specific &nbsp; '''STDERR''' &nbsp; output steam (or a method of writing to one), &nbsp; or have the &nbsp; '''stderr''' &nbsp; device driver
<br>disabled &nbsp; (the "driver" may be known as a different name), &nbsp; or if the REXX doesn't utilize or bypasses the &nbsp; '''stderr''' &nbsp; device
<br>or don't have a specific &nbsp; '''STDERR''' &nbsp; output steam
(or a method of writing to one), &nbsp; or have the &nbsp; '''stderr''' &nbsp; device driver
<br>driver. &nbsp; Some or all of the above restrictions generally refer to the older/legacy REXXes.
<br>disabled &nbsp; (the "driver" may be known as a different name), &nbsp; or if the REXX doesn't utilize or bypasses the &nbsp; '''stderr''' &nbsp; device driver.<br>
Some or all of the above restrictions generally refer to the older/legacy REXXes.
<br>The output (below) is goes to the terminal/console &nbsp; (which is the destination for all normal and error messages).
<br>The output (below) is goes to the terminal/console &nbsp; (which is the destination for all normal and error messages).
<lang rexx>say 'Goodbye, World!'</lang>
<lang rexx>say 'Goodbye, World!'</lang>


===version 2===
===version 2===
This version will work with those operating systems (hosts) that support stream output and a &nbsp; STDERR &nbsp; output
This version will work with those operating systems (hosts)
that support stream output and a &nbsp; STDERR &nbsp; output
<br>stream (by name).
<br>stream (by name).
<br>If the &nbsp; '''stderr''' &nbsp; name is supported and enabled, the output is written to the terminal.
<br>If the &nbsp; '''stderr''' &nbsp; name is supported and enabled, the output is written to the terminal.
Line 624: Line 667:
document.write('Goodbye, World!');
document.write('Goodbye, World!');
</script>""</lang>
</script>""</lang>
Run Basic runs in a browser. This opens a new browser window, or a tab in the case of Chrome and some others.
Run Basic runs in a browser.
This opens a new browser window,
or a tab in the case of Chrome and some others.


=={{header|Rust}}==
=={{header|Rust}}==
Line 672: Line 717:


=={{header|Scala}}==
=={{header|Scala}}==
[[Category:Scala Implementations]]
{{libheader|Console}}
{{libheader|Console}}

===Ad hoc REPL solution===
===Ad hoc REPL solution===
Ad hoc solution as [http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop REPL] script:
Ad hoc solution as [http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop REPL] script:
<lang Scala>Console.err.println("Goodbye, World!")</lang>
<lang Scala>Console.err.println("Goodbye, World!")</lang>

===Via Java runtime===
===Via Java runtime===
This is a call to the Java run-time library. '''Not recommendated'''.
This is a call to the Java run-time library. '''Not recommendated'''.
<lang Scala>System.err.println("Goodbye, World!")</lang>
<lang Scala>System.err.println("Goodbye, World!")</lang>

===Via Scala Console API===
===Via Scala Console API===
This is a call to the Scala API. '''Recommendated'''.
This is a call to the Scala API. '''Recommendated'''.
<lang Scala>Console.err.println("Goodbye, World!")</lang>
<lang Scala>Console.err.println("Goodbye, World!")</lang>

===Short term deviation to err===
===Short term deviation to err===
<lang Scala>Console.withOut(Console.err) { println("This goes to default _err_") }</lang>
<lang Scala>Console.withOut(Console.err) { println("This goes to default _err_") }</lang>

===Long term deviation to err===
===Long term deviation to err===
<lang Scala> println ("Out not deviated")
<lang Scala> println ("Out not deviated")
Line 702: Line 751:
}</lang>
}</lang>


This program requires at least 1 line of input. It changes the first line to "Goodbye, World!" and then prints the first line to standard error. It reads and ignores the remaining lines.
This program requires at least 1 line of input.
It changes the first line to "Goodbye, World!"
and then prints the first line to standard error.
It reads and ignores the remaining lines.


Test output: <lang bash>$ echo a | sed -f error.sed >/dev/null
Test output: <lang bash>$ echo a | sed -f error.sed >/dev/null
Line 731: Line 783:
=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>puts stderr "Goodbye, World!"</lang>
<lang tcl>puts stderr "Goodbye, World!"</lang>

=={{header|TUSCRIPT}}==
=={{header|TUSCRIPT}}==
<lang tuscript>
<lang tuscript>
Line 738: Line 791:
PRINT/ERROR text
PRINT/ERROR text
</lang>
</lang>
{{out}}
Output:
<pre>
<pre>
@@@@@@@@ hello world @@@@@@@@
@@@@@@@@ hello world @@@@@@@@
Line 751: Line 804:
<lang csh>echo "Goodbye, World!" >/dev/stderr</lang>
<lang csh>echo "Goodbye, World!" >/dev/stderr</lang>


This requires <code>/dev/stderr</code>, a device node from [[BSD]] and some other Unix clones. This command works with both Bourne Shell and C Shell.
This requires <code>/dev/stderr</code>, a device node from [[BSD]]
and some other Unix clones.
This command works with both Bourne Shell and C Shell.


=={{header|X86 Assembly}}==
=={{header|X86 Assembly}}==
Line 778: Line 833:


=={{header|XPL0}}==
=={{header|XPL0}}==
The terms "standard output" and "standard error" are not used, but it's
The terms "standard output" and "standard error" are not used,
trivial to send messages to a variety of devices by specifying their
but it's trivial to send messages to a variety of devices
by specifying their numbers.
numbers. Normally messages are displayed on the text console, which is
Normally messages are displayed on the text console, which is device 0.
device 0. Instead, this example sends the message to the (first) printer,
Instead, this example sends the message to the (first) printer,
which is device 2.
which is device 2.


Line 789: Line 845:
=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>File.stderr.writeln("Goodbye, World!")</lang>
<lang zkl>File.stderr.writeln("Goodbye, World!")</lang>


{{omit from|Applesoft BASIC}}
{{omit from|bc|Always prints to standard output.}}
{{omit from|Brainf***}}
{{omit from|dc|Always prints to standard output.}}
{{omit from|GUISS|Cannot customize error messages}}
{{omit from|Integer BASIC}}
{{omit from|TI-83 BASIC|Same reason as TI-89.}}
{{omit from|TI-89 BASIC|no analogue to stderr, unless you count graph display vs. program IO}}
{{omit from|Unlambda|No concept of standard error (or alternate output streams in general).}}

[[Category:Streams]]