• Post Reply Bookmark Topic Watch Topic
  • New Topic

Random order of printing by printStacktrace()  RSS feed

 
Sushant Kunwara
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the java code that i compiled on eclipse but each time i run it i am getting different sequence of output !!! Why ??

What I did is that my main() calls a function m1() which calls a function m2() which throws an exception back to m1() which throws exception back to main().



The output is as shown in screenshots at different times :

SCREENSHOT 1:


SCREENSHOT 2:


SCREENSHOT 3:


How is this possible ??
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't call your methods functions; they are called methods.

You might be getting the two printouts in different order because of a delay in passing the stack trace to the console.
Or: the JVM can see that it has to print something and throw an Exception. If there is a delay getting the printout to the console, it is permitted to optimise itself by waiting for the console to become available, and throwing the Exception during the wait. Once you are in a threaded environment, which includes Eclipse, you are outwith the realms of determinism and things may happen in a different order from what you expect.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, the finally block prints to System.out and the Exceptions are printed to System.err. Those streams may run on different threads, and they are notorious for not running in any particular order.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!