• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting a different output everytime a catch block is executed.  RSS feed

 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Sorry in advance if this is a stupid question.

Could somebody please explain why the order of lines in the output varies everytime I execute the following program.




I have received three different output cases. They are as follows.

run:
java.lang.Exception: Exception Again
hello
Not Terminated yet
at TestSimple.ExceptionTest2.workonExceptions(ExceptionTest2.java:25)
at TestSimple.ExceptionTest2.main(ExceptionTest2.java:14)
BUILD SUCCESSFUL (total time: 0 seconds)

run:
hello
java.lang.Exception: Exception Again
Not Terminated yet
at TestSimple.ExceptionTest2.workonExceptions(ExceptionTest2.java:25)
at TestSimple.ExceptionTest2.main(ExceptionTest2.java:14)
BUILD SUCCESSFUL (total time: 0 seconds)

run:
hello
Not Terminated yet
java.lang.Exception: Exception Again
at TestSimple.ExceptionTest2.workonExceptions(ExceptionTest2.java:25)
at TestSimple.ExceptionTest2.main(ExceptionTest2.java:14)
BUILD SUCCESSFUL (total time: 0 seconds)

I just don't see why stackprinttrace should start before hello is printed. Also in the third run, even 'Not Terminated yet' is printed before stacktrace has started. I can't understand this behaviour. Is this related to threads? Does printstacktrace run as a separate thread or something? Could somebody please advice.

Thanks.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The stack traces are going to System.err, but your println()s are going to System.out. Those two Streams (Writers?) can dump their bytes out to the console independently of each other, without regard to the order in which they were filled up.

WIthin a given stream--out or err--the messages will be in order, but between the two, they can be interleaved.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Jeff.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!