File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 839
    
  14
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Different output while using printStackTrace()
 
Similar Threads
finally block with return statement
Exception Q
throws exception
return statement effect in Exception class
explain the output