Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

stange sequence of output in eclipse IDE

 
netto gaurav
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm trying to run a simple scenario but eclipse is giving me different output each time, refer to the code below -


sometimes I'm getting the output as below -
In catch
java.lang.NullPointerExceptionIn finally

at practice.exception1.main(exception1.java:9)

and at times -
In catch
In finally
java.lang.NullPointerException
at practice.exception1.main(exception1.java:9)


whereas as per my understanding the correct output should be -
In catch
java.lang.NullPointerException
at practice.exception1.main(exception1.java:9)
In finally

Am I missing something over here or is this some problem with eclipse. I tried running the program through command prompt and it worked fine (output as per my expectation)

Looking forward to a reply.
Thanks





 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that it's the result of the fact that writing to console is a slow operation (also blocking one). Did you try to redirect your output to file and see the result you get?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because the exception stack trace and what you print in your program using System.out.println() are sent to two different outputs: the exception is printed to stderr, while your System.out.println output is printed to stdout. You see in the Eclipse console that the exception message is in red, while your output is in black.

Those two output streams are separate, and Eclipse probably has two different threads to handle them, which print the result independently of each other in Eclipse's console window. Because that happens independently, the order in which the messages appear in the window isn't predictable (it depends on thread scheduling).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic