Why does the execution order between the printStackTrace() and the other methods appear to be nondeterministic?
Asked Answered
S

1

6

In my code snippet below, the printStackTrace() method is called in the catch block. After running the program you can see that sometimes the printStackTrace() runs several times in a row instead of running in the order of printStackTrace() --> catch block --> finally block.

If you change the static boolean b to false then the System.out.print(e) executes in order.

So why does the printStackTrace() behaves in different way? (something with the threads??)

public class PrintStackTrace {
    static boolean b = true;
    public static void main(String[] args){
        for(int i = 0; i < 100; i++){
            try{
                throw new Exception("[" + i + "]");
            }
            catch(Exception e){
                if(b){
                    e.printStackTrace();
                }
                else{
                    System.out.print(e);
                }
                System.out.print(" Catch: " + i);
            }
            finally{
                System.out.print(" Finally: " + i);
            }
            System.out.println();
        }
    }
}
Sapphirine answered 11/5, 2014 at 1:50 Comment(1)
If you add System.out.flush(); System.err.flush(); lines to your code, the output will be sequential as expected.Katzen
E
16

It is because printStackTrace writes in System.err while System.out.println writes on System.out. Even if both System.err and System.out use the same underlying resource for the output messages (e.g. the same file or the same console), they flush at different moments.

If you want to have a synchronized output, write the exceptions in System.out as well:

e.printStackTrace(System.out);

Or even better, use a logger, which already synchronizes the outputs into a shared resource and give you more options about what's being output in the message e.g. Class, method, date and time, thread name, etc. among other benefits like writing log messages in database instead of a text file, and on.

Ellington answered 11/5, 2014 at 1:55 Comment(3)
A point that the OP could have easily determined by reading the description of printStackTrace.Inferno
@HotLicks i did check the implementation but still I did not know that System.err could be the answer. that is why we help each other :)Sapphirine
highly relevantOliana

© 2022 - 2024 — McMap. All rights reserved.