简体   繁体   English

Java处理stackoverflow,并在stackoverflow错误后继续正常执行

[英]Java dealing with stackoverflow and continue normal execution after stackoverflow error

I'm trying to do a recursion in Java. 我正在尝试用Java进行递归。 I just want to stop the recursion and continue normal prgram execution 我只想停止递归并继续正常的prgram执行

void doit(){
    try{
        doit();
    }
    catch (StackOverflowError e) {
        return;
    }
    System.out.println("Error");
}


statement1
doit()
statementcontinue

I want the program to continue execution to statementcontinue after the stackoverflow error 我希望程序在stackoverflow错误之后继续执行到statementcontinue

Your program is doing exactly what you told it to. 您的程序完全按照您的指示进行。

Each time you call doit() , it: 每次调用doit() ,它:

  1. Calls doit() again 再次调用doit()
  2. After that finishes, it prints Error . 完成后,将显示Error

When the stack overflow happens, the innermost call finishes (because of your return ), and then continues executing the function that called it (like any other function call). 当堆栈溢出发生时,最里面的调用结束(由于您的return ),然后继续执行调用它的函数(就像其他任何函数调用一样)。
This is called popping the call stack. 这称为弹出调用堆栈。

The calling function (which is also doit() ) then executes the next line ( System.out.println("Error"); ), then returns to its calling function, which is also doit() . 然后,调用函数(也是doit() )执行下一行( System.out.println("Error"); ),然后返回调用函数也是doit()
The cycle repeats until the stack is fully popped – until it gets up tothe function that originally called doit() . 循环重复进行,直到堆栈完全弹出为止-直到它到达最初称为doit()函数为止。

If you want to print "Error" only when the stackOverflow occurs, just place the trace in the catch block: 如果仅在发生stackOverflow时才打印“错误”,只需将跟踪放置在catch块中:

void doit(){
  try{
    doit();
  }catch (StackOverflowError e) {
    System.out.println("Error");
    return;
  }
}

Your code fills up the stack, then once the stack is full it hits the catch statement. 您的代码将填满堆栈,然后,一旦堆栈已满,它将命中catch语句。 After that the rest of the code continues to fire... each of those error messages is a recursive call that was made. 此后,其余代码继续触发...这些错误消息中的每一个都是进行的递归调用。 Your code is working as you programmed it. 您的代码在编程时正在工作。

If you want an example of recursion that does stuff before and after, and has an exit condition, the following should work as an example for you (with print statements to clarify what is happening on the stack). 如果您想要一个递归示例,该递归示例在前后进行填充,并且具有退出条件,那么以下示例应该为您工作(使用print语句来阐明堆栈中正在发生的事情)。

Example: 例:

public class RecurseExample {

    public static void main(String[] args) {
        System.out.println("hi");
        doIt(1);
        System.out.println("bye");
    }

    private static void doIt(int i){
        if (i <= 3){
            System.out.println("i before: " + i);
            doIt(++i);
            System.out.println("i after: " + i);
        } else {
            System.out.println("this is where the recursion stops and the execution will continue to unravel the stack");
        }
    }
}

The output: 输出:

hi
i before: 1
i before: 2
i before: 3
this is where the recursion stops and the execution will continue to unravel the stack
i after: 4
i after: 3
i after: 2
bye

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM