unwind to the first frame of a recursive call in Java?

551 Views Asked by At

Suppose I have a (very simple) recursive method like this:

public static void myMeth(int n)
{
     // do something

     // now execute the recursive call
     if (n < 0) return;
     else if ( n == SOME_CONST ) throw new UnsupportedOperationException();
     else myMeth(n - 1);
}

(The second condition n == SOME_CONST is just there to make the point that sometimes an exception can occur, sometimes it does not).

Suppose I call myMeth(10), and that the exception does happen after a few recursive calls (say SOME_CONST == 5).

Is there any trick I could do (with try-catch block, that is) to get me back to the first frame of myMeth ?

5

There are 5 best solutions below

2
On

Yes, but this kind of trick will miss the whole concept of the recursive and will be hard to read and understand. You shouldn't use recursive if you can't expect the definite number of options it can produce.

Otherwise use another solution.

1
On
try{

     myMeth(n);

catch (UnsupportedOperationException e) {
 myMeth(n); //or another number
}
3
On

This could work, there is probably a cleaner solution out there, but it's a start:

public static void myMeth(int n, boolean firstCall)
{
     // do something

     // now execute the recursive call

     try
     {
         if (n < 0) return;
         else if ( n == SOME_CONST ) throw new UnsupportedOperationException();
         else myMeth(n - 1, false);
     }
     catch(UnsupportedOperationException e)
     {
         if (firstCall)
         {
              //logic
         }
         else
         {
              throw e;
         }
     }
}
0
On

Using another static variable to keep track of the first number (10)

    static int SOME_CONST = 5;
    static int keepN;

    public static void myMeth(int n) {
        // do something

        // now execute the recursive call
        try {
            if (n < 0) {
                return;
            } else if (n == SOME_CONST) {
                throw new UnsupportedOperationException();
            } else {
                myMeth(n - 1);
            }
        } catch (UnsupportedOperationException e) {
            if (n == keepN) {
                System.out.println(e);
                System.out.println("YES first frame");
            } else {
                System.out.println("NO");
                throw e;
            }
        }
    }

    public static void main(String[] args) {
        keepN = 10;
        myMeth(10);
    }
0
On
// depth should be 0 on first call
public static boolean myMeth(int n, int depth)
{
     // do something

     // now execute the recursive call
     if (n < 0) return true;
     else if ( n == SOME_CONST ) return false;
     boolean success = myMeth(n - 1, depth + 1);
     if (depth == 0 && !success) {
         // uh-oh
     }
     return success;
}

Or if you don't care about each individual frame in the recursion, replace depth with a boolean and change to boolean success = myMeth(n - 1, false);

I'm not sure what you're asking when you say you want to get back to the first frame though. Do you want to go back to the beginning of the first method call, so you can repeat the steps in the // do something block? Or are you fine executing right after the recursive call to myMeth?

If you're generating the Exception yourself, I replaced the need for that by using booleans. If not, you can replace it. You could also just throw an exception in the first frame, while still using booleans.