aspose file tools*
The moose likes Beginning Java and the fly likes Strange Exception Behaviour Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strange Exception Behaviour" Watch "Strange Exception Behaviour" New topic
Author

Strange Exception Behaviour

Arthur Blair
Ranch Hand

Joined: Sep 20, 2005
Posts: 71
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

Joined: Sep 16, 2005
Posts: 1780
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.


There is no emoticon for what I am feeling!
Arthur Blair
Ranch Hand

Joined: Sep 20, 2005
Posts: 71
...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

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Sep 20, 2005
Posts: 71
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

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Aug 03, 2004
Posts: 245
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

Joined: Sep 16, 2005
Posts: 1780
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange Exception Behaviour
 
Similar Threads
Question for Kathy on Unreachable code
exception handling-explain output
Trouble with an Applet Exception.
sms gateway
Image smoothing