Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between "out" and "err"

 
Saurabh Saha
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All!
Please tell me what is the difference between out and err object. I know that out use for OutputStream and err use for Error. we use err object in the exception. but i'm not sure where to use err because most of the time we use out object. They give the same output like-

System.out.println("Hello");
and
System.err.println("Hello");

Please clear my doubt.

Thanks & Regards,
Vivek Mishra
 
Sanjaya Sugiarto
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By convention, the System.err used to display error message.
 
Henry Wong
author
Marshal
Pie
Posts: 21116
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In most cases, it makes little difference. I have rarely seen applications that route standard out and err to different locations. Maybe it is because it is hard to debug an error, without the output of the program.

Henry
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main issue is that System.out may be redirected at either the Java or the operating system level to another device, while you still want to see the error mssages on the console. Or the reverse, if you want to log all error messages to disk.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A bigger issue is that the standard-issue Windows command shell historically didn't, and perhaps still doesn't, let you redirect stderr to a file, making programs that use system.err for logging painful to work with.
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I seem to recall that another difference is in the timing in which they are executed. For instance when I was learning threads I think they suggested using err because it was more reliable in terms of the timing of when it is written to the console. It's been a while and I could be wrong but I think that was the case.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Hentay Duke:
I seem to recall that another difference is in the timing in which they are executed.


It's indeed true that the err is often unbuffered (i.e., output appears immediately) and out is often buffered (i.e., output appears after newline, or call to flush().) But I believe this is an unspecified platform detail. I'm not 100% certain of that.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also heard about the timing difference but, checking the source of java.lang.System, I see they now both call flush() after every println() in the Wintel version. This isn't covered in any doc I could find.
 
Jason Fox
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at the Java 1.4 API, the only difference seems to be that (without using the set methods to redirect either) out prints to stdout (standard out) and err prints to stderr (standard error). On most systems, this will be the same thing most of the time. On my Linux box, I have tty10 setup to print out all stderr messages, and some logging applications may use stderr. One use I think is that you can redirect stderr to an error log, and then anytime the developer used System.err.print(ln) to indicate an error, it will show up in the logfile. Could somebody point me to where I can find information on wether out or err is flushed and when?
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can see in the source code for java.lang.System that both err and out are flushed after every println()
 
Sheetal Kaul
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

General Idea ::
---------------

Probably the most often used items from the System class are the the standard output and standard error streams, which you use to display text to the user. The standard output stream is typically used for command output, to display the results of a command to the user. The standard error stream is typically used to display any errors that occur when a program is running.

Explaination ::
---------------
System.out leads the output to the standard output stream
(normally mapped to your console screen), System.err leads the
output to the standard error stream (by default the console, too).
The standard output should be used for regular program output,
the standard error for errormessages. If you start your console
program regularly both message types will appear on your screen.

But you may redirect both streams to different destinations (e.g. files),
e.g. if you want to create an error log file where you don't want to be
the regualr output in.

On an UNIX you may redirect the output as follows:

java yourprog.class >output.log 2>error.log

this causes your regular output (using System.out) to be stored in
output.log and your error messages (using System.err) to be stored
in error.log

System.err is a "special" pipe that usually is directed
to the standard consolle. You can redirect the System.out with the
normal pipe control (| or > , but System.err no. If you want to put
both the "normal" output and the "error" output to a file you must
use the special redirect 2>.

This allow you to send normal messages into a file or in the /null
black hole, but still receive the error messages on the console.

Thanxs
Sheetal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic