• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

System.err or System.out

 
Michelle Lee
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have one question:
What is the difference between
System.err.print() or System.out.print().
Both use to print to standard output.
they seems behaviours same.
Can anybody explain to their difference? Thank you!
[ January 22, 2003: Message edited by: michelle Lee ]
 
Nayanjyoti Talukdar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In some applications, output is redirected to a file. In that scenario, System.err.println() is used to display the error messages in the console. If we use System.out.println(), then all the messages will be redirected to the specified file. This is done in order to prevent the error messages being displayed in the redirected file.
------------
Nayan.
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
Thanks Talukdar ,
I have writen a sample code to test your suggestion hope it will be useful to understand
Type this in the command prompt while executing code.
java ErrStreamCheck >> test.txt
Error free output will go into file 'test.txt' and error records get printed into consol.
public class ErrStreamCheck
{
public static void main(String args[])
{
String strArr[] = { "p1","p2","p3" };
for(int i =0 ; i<5 ; i++ )
{
try
{
System.out.println("array content " + strArr[i] );
}
catch( Exception oEx )
{
System.err.println("Exception due to wrong index.");
}
}
}
}
[ January 22, 2003: Message edited by: Arun Boraiah ]
[ January 22, 2003: Message edited by: Arun Boraiah ]
 
Ken Cobbs
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Arun,
That example was great! Putting that in my memory bucket( on disk )
 
Nayanjyoti Talukdar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice example, Arun
I forgot to mention example!!
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.err does not print to standard out (as stdout); it prints to standard error (stderr). Normally, these are both the console, but as shown in the above example it doesn't have to be. I think these ideas stem from Unix. At least that's where I have become the most familiar with them.
HTH
Layne
 
Nayanjyoti Talukdar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Normally, these are both the console, but as shown in the above example it doesn't have to be.

what do u mean by that? Do u meanit shouldn't behave like that?
--------------
Nayan
 
Michelle Lee
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all! javaranch is really great, you
can learn a lot from here.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic