printStackTrace()
acts as if it runs in its own thread after waiting for input. Here's my code:
try {
new Scanner(System.in).nextLine();
throw new Exception();
} catch (Exception e) {
e.printStackTrace();
}
System.out.print("STUFF");
I sometimes get the expected output (with STUFF
at the end), but sometimes I get this:
blabla // the scanner input
java.lang.ExceptionSTUFF
at Test.main(Test.java:7)
and sometimes this:
blabla
java.lang.Exception
STUFF at Test.main(Test.java:7)
Replacing the scanner with System.in.read()
yields the same results. Removing the line entirely yields the expected result. In the actual program where I noticed this problem, the stack trace was much longer, and the STUFF
output always appeared either as expected or as in the second output above - at the beginning of the second line.
What's causing this, and how can I solve it?