Joseph
Joseph

Reputation: 85

Print Stack Trace intefering with Print Line

Sorry if this is a nooby question.

When using System.out.println() followed by printing a stack trace, the two seem to overlap and interfere with eachother.

This code:

System.out.println("Multiple definitions for " + analyzer.name + ":");
for (String name : resultNames) {
    System.out.println('\t' + name);
}
throw new RuntimeException("Multiple class definitions found matching " + analyzer.name);

produces this output

Exception in thread "main" java.lang.RuntimeException: Multiple class definitions found matching RenderableNode
Multiple definitions for RenderableNode:
    at org.lime.apollo.updater.UpdaterApplication.runAnalyzer(UpdaterApplication.java:63)
    Analyzer1
    Analyzer2
    at org.lime.apollo.updater.UpdaterApplication.runAnalyzers(UpdaterApplication.java:70)
    Analyzer3
    at org.lime.apollo.updater.UpdaterApplication.main(UpdaterApplication.java:87)

The expected behavior is for Analyzer1, Analyzer2, etc to be printed before any of the stack trace. But instead the two seem to be printing on top of eachother, which makes a result that is hard to read.

What am I doing wrong?

Edit: Kon's answer below explains this, but I don't know how to mark as solved.

Upvotes: 2

Views: 265

Answers (1)

morgantaschuk
morgantaschuk

Reputation: 216

System.out.println is buffered whereas error output is not buffered. That means that there is occasionally a delay before you see the results from System.out.println. Exceptions are reported through System.err.println, which is not buffered and so prints immediately.

Try switching your System.out.println statements to System.err.println, and see if the problem resolves.

Upvotes: 2

Related Questions