Forums Register Login

Getting a different output everytime a catch block is executed.

+Pie Number of slices to send: Send
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.

1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thanks, Jeff.
Please enjoy this holographic presentation of our apocalyptic dilemma right after this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 661 times.
Similar Threads
Generics - need help
Call by Value
Help with english to morse code program, I have no idea what I'm doing
explain the output
declaring throws Exception after main()
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:20:03.