Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange Exception Behaviour

 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having some unexpected behaviour when I try to run a basic exception example in Netbeans. It seems as though System.err is acting strangely.

For example, when I run the following code it doesn't seem to run in the expected sequence...



The output is this:



But why does it group the output of the printStackTrace() calls for f() and h() together? Shouldn't these be printed within the seperate catch blocks within which they are called?

Thoughts would be appreciated.

[ November 14, 2005: Message edited by: Arthur Blair ]
[ November 14, 2005: Message edited by: Arthur Blair ]
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you mixing calls to System.out and System.err? I think that
is the source of the confusion, and that it has nothing to do with exceptions.
 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...because it seems to temporarily solve my problem.

If I use e.printStackTrace(System.out) in the catch blocks of f(), g() and h() everything works as I would have expected.

But if, I remove the System.out from g() and just print to System.err, the output for g() ang f() gets batched together:



I'm interested to know why this is the case.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... I'm writing everything to System.err:

And I get this output:
[PRE]
Throwing MyException2 from f()
MyException2: Detail Message: 0 null
at ExtraFeatures.f(ExtraFeatures.java:5)
at ExtraFeatures.main(ExtraFeatures.java:20)
Caught f()
f() e.val(): 0
f() e.getMessage(): Detail Message: 0 null

Stop inbetween the guarded areas

Throwing MyException2 from g()
MyException2: Detail Message: 0 Originated in g()
at ExtraFeatures.g(ExtraFeatures.java:10)
at ExtraFeatures.main(ExtraFeatures.java:30)

Throwing MyException2 from h()
MyException2: Detail Message: 47 Originated in h()
at ExtraFeatures.h(ExtraFeatures.java:15)
at ExtraFeatures.main(ExtraFeatures.java:35)
[/PRE]
[ November 14, 2005: Message edited by: Jeff Albrechtsen ]
 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried running the same code in Eclipse and I get something different again.



...has it got something to do with System.out and System.err running as seperate threads?
[ November 14, 2005: Message edited by: Arthur Blair ]
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Woops, I guess that line should have used System.err as well:

The vagaries of the output are because these two streams are not tied together:
they may be buffered (separately) so how they combine is not determined.
Perhaps it's best to imagine (or make!) System.out and System.err write to different files.
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Netbeans probalbly does not synchronize system.err and system.out reading.
These streams are independent and lava.lang.Process has no way of reading data from both streams in order. So if Netbeans uses it, it can't avoid such problems.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moral: don't "temporarily solve" problems by writing to a mix of System.out and
System.err.
Think of them as separate files, because they are!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic