1

I'm trying to do a recursion in Java. I just want to stop the recursion and continue normal prgram execution

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

Noor
  • 19,638
  • 38
  • 136
  • 254
  • You should not be attempting to `catch` an `Error`, only `Exception`s ... recovering from errors is often not possible. http://stackoverflow.com/a/912352/346561 – Jesse Webb Apr 18 '13 at 19:37
  • If you want to verify that it is working properly, you can put a counter into your program and have it print the counter value in the catch statement (showing how many times it recursed), then put a sleep for 5 seconds after that print statement. This will let you see how many times it recursed, then after 5 seconds it will print all of the error messages which will match the count total you see. If you are looking to control the exit of recurion, then you need a conditional (if/then) which can determine if it needs to recurse further or not. – James Oravec Apr 18 '13 at 19:51
  • @user1774937, I updated my answer to show you an example that does work as you want. – James Oravec Apr 18 '13 at 19:59

3 Answers3

3

Your program is doing exactly what you told it to.

Each time you call doit(), it:

  1. Calls doit() again
  2. After that finishes, it prints 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).
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().
The cycle repeats until the stack is fully popped – until it gets up tothe function that originally called doit().

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • The text only starts printing once the StackOverflowError is thrown. – le3th4x0rbot Apr 18 '13 at 19:35
  • @BaileyS: Yes; I thought I said that. – SLaks Apr 18 '13 at 19:35
  • in fact, my code is much more complication, I'm trying to do something i know will fail, I'm exploiting all hyponyms from wordnet. ITs a big method that updates a list, that list is an instance variable. I just want that the when getting this error, the program stops that I can continue processing using the list of words – Noor Apr 18 '13 at 19:35
  • @user1774937: And therefore what? Do you have a further question? – SLaks Apr 18 '13 at 20:16
2

If you want to print "Error" only when the stackOverflow occurs, just place the trace in the catch block:

void doit(){
  try{
    doit();
  }catch (StackOverflowError e) {
    System.out.println("Error");
    return;
  }
}
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
1

Your code fills up the stack, then once the stack is full it hits the catch statement. 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).

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
James Oravec
  • 19,579
  • 27
  • 94
  • 160
  • You are actually setting a predefined bound, in my case, I'm performing query expansion using hyponyms in wordnet, I try to expand as far as possible, I stop only when no further expansion is possible based on a word or when the stackoverflow error occurs, is this possible?? – Noor Apr 19 '13 at 21:44