This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes Different output while using printStackTrace() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Different output while using printStackTrace()" Watch "Different output while using printStackTrace()" New topic
Author

Different output while using printStackTrace()

Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
I am executing following code:



I get different output in different runs.

optput

Computing average.
java.lang.ArithmeticException: Integer division by 0
at com.khalid.ch5.Average7.computeAverage(Average7.java:36)
at com.khalid.ch5.Average7.printAverage(Average7.java:20)
at com.khalid.ch5.Average7.main(Average7.java:7)
Finally in printAverage().
Exception handled in main().
Finally in main().
Exit main().


========================

java.lang.ArithmeticException: Integer division by 0
at com.khalid.ch5.Average7.computeAverage(Average7.java:36)
at com.khalid.ch5.Average7.printAverage(Average7.java:20)
at com.khalid.ch5.Average7.main(Average7.java:7)
Computing average.
Finally in printAverage().
Exception handled in main().
Finally in main().
Exit main().


=========================

Computing average.
java.lang.ArithmeticException: Integer division by 0
Finally in printAverage().
at com.khalid.ch5.Average7.computeAverage(Average7.java:36)
at com.khalid.ch5.Average7.printAverage(Average7.java:20)
at com.khalid.ch5.Average7.main(Average7.java:7)
Exception handled in main().
Finally in main().
Exit main().

==============================

Computing average.
Finally in printAverage().
Exception handled in main().
Finally in main().
Exit main().
java.lang.ArithmeticException: Integer division by 0
at com.khalid.ch5.Average7.computeAverage(Average7.java:36)
at com.khalid.ch5.Average7.printAverage(Average7.java:20)
at com.khalid.ch5.Average7.main(Average7.java:7)


I am assuming that a new thread is being spawned for printing stack trace, so depending upon main/new thread currently active, the output is displayed. I am using jdk 1.6. Is my assumption right?


SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1034
    
  15
The stack traces are going to System.err and System.out.println statements are going to System.out and both of them are dumping the content onto the same console. Each of these streams individually dump the content onto the same console without any consideration of the other stream. But within each stream, the messages will be printed in order.

HIH,
Chan.
 
wood burning stoves
 
subject: Different output while using printStackTrace()