【问题标题】:How this java program handles a StackOverFlow error and continues to normal flow of execution [duplicate]这个java程序如何处理StackOverFlow错误并继续正常的执行流程[重复]
【发布时间】:2019-01-18 10:32:55
【问题描述】:

在下面的代码中,方法A()B() 递归调用对方导致StackOverFlow 错误。捕获此错误后,由于堆栈已溢出并且需要将方法 C()->D()->E() 放入调用堆栈,程序如何继续其正常的执行流程。

package staticTryOuts;

class Test
{
    public static void main(String[] args) {
       try {
        A();
       }catch(Error e) {
           System.out.println(e);
       }
        System.out.println("Hello");
        C();

    }

    static void A() {
        System.out.println("Hello from A");

        B();
    }
    static void B() {
        System.out.println("Hello from B");

        A();
    }
    static void C() {
        System.out.println("Hello World from c.");
        D();
    }
    static void D() {
        System.out.println("Hello world from D");
        E();
    }
    static void E() {
        System.out.println("Hello world from E");

    }
}

【问题讨论】:

    标签: java exception


    【解决方案1】:

    抛出的异常意味着它会在堆栈中向上传播,同时展开和清除堆栈帧。当异常返回到最顶部的catch 块时,堆栈基本上又是空的,并且有空间用于CDE 的方法调用的附加堆栈帧。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-12
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      • 1970-01-01
      • 2012-05-19
      • 2021-06-25
      • 1970-01-01
      相关资源
      最近更新 更多