Search code examples
javanetbeansnetbeans-8.1

Netbeans 8.1 & Java : Messed up output when error is thrown


I noticed that Netbeans messes up the entire output once an error is thrown. It makes the output look like this incomprehensive mess:

Pushing elements onto doubleStack
1.1 2.2 3.3 4.4 5.5 6.6 
Exceptions.FullStackException: Stack is full, cannot push 6.6

Popping elements from doubleStack
5.5 4.4 3.3 2.2 1.1 

    at domein.Stack.push(Stack.java:37)
Pushing elements onto integerStack
    at StackApplicatie2.testPush(StackApplicatie2.java:40)
1 2 3 4 5 6 7 8 9 10 11 

    at StackApplicatie2.testStacks(StackApplicatie2.java:24)
Popping elements from integerStack
    at StackApplicatie2.main(StackApplicatie2.java:75)
<etc. …>

instead of what one would expect:

Pushing elements onto doubleStack
1.1 2.2 3.3 4.4 5.5 6.6 
Exceptions.FullStackException: Stack is full, cannot push 6.6
    at domein.Stack.push(Stack.java:37)
    at StackApplicatie2.testPush(StackApplicatie2.java:40)
    at StackApplicatie2.testStacks(StackApplicatie2.java:24)
    at StackApplicatie2.main(StackApplicatie2.java:75)

Popping elements from doubleStack
5.5 4.4 3.3 2.2 1.1 

Pushing elements onto integerStack
1 2 3 4 5 6 7 8 9 10 11 

Popping elements from integerStack
<etc. …>

I was just wondering: is this normal for Netbeans 8.1 to give such a weird ouput or not?


Solution

  • When you use exception.printStackTrace(), then the stack trace is printed to System.err, your own messages are printed to System.out (I guess). Both are buffered streams, and only when those streams are flushed (buffer size reached, newlines, etc), the output appears at the destination.

    In a simple terminal applications, both streams are directed at the console, however the buffering of both streams has the effect that flushes can interleave, and therefor the output of both streams can get mixed up.

    This, btw, is not specific to Netbeans.

    If for some reasons you really want them in order, and don't want to write the exception stacktrace to the error stream, then you could use exception.printStackTrace(System.out). However be aware that it is unusual to do so; exceptions should be exceptional and should usually not go to the 'standard output'.