Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

java - Why does the execution order between the printStackTrace() and the other methods appear to be nondeterministic?

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();
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...