This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Different output while using printStackTrace()

 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Chan Ag
Rancher
Posts: 1089
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic