Understanding the Java stack

前端 未结 7 1135
无人及你
无人及你 2020-12-24 06:44

There is this code:

public class Main {
    public static void main(final String[] args) throws Exception {
        System.out.print(\"1\");
        doAnythi         


        
7条回答
  •  礼貌的吻别
    2020-12-24 07:28

    Well the no. of times the stack overflow error is hit is undefined. However, the JVM allows you to recover from StackOverflowError error and continue execution of the system normally.

    It is proved by the following code:

    public class Really {
       public static void main(final String[] args) throws Exception {
         System.out.print("1");
         doAnything();
         System.out.println("2");
       }
       private static void doAnything() {
        try {
               throw new StackOverflowError();
           //doAnything();
        }
        catch(final Error e){
            System.out.print("y");
        }
       }
    }
    

    Note however, as @Javier said, the StackOverflowError is thrown by the JVM synchronously or asynchronously(which means it can be thrown by another thread, possibly a native thread) which is why it is not possible to get the stack trace of the error. The no. of times the thread(s) hit the catch() block is undefined.

提交回复
热议问题