1

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 ?

One Two Three
  • 22,327
  • 24
  • 73
  • 114

5 Answers5

1

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;
         }
     }
}
Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138
  • Hi Kevin, he wants to "go back" like a jmp assembler instruction. Not to handle the exception... – Typo Jan 16 '14 at 20:01
  • He does want to handle the exception, he just wants to handle it on the first recursive call, this would essentially bubble the exception up to the first call. I doubt you are going to get anything resembling a jmp instruction within java. – Kevin DiTraglia Jan 16 '14 at 20:06
  • I know, but here's what it's posted and i quote: "...to get me back to the first frame of myMeth...". Get back doesn't mean handle the exception in the first iteration (sorry @OneTwoThree "frame"). – Typo Jan 17 '14 at 00:48
0
try{

     myMeth(n);

catch (UnsupportedOperationException e) {
 myMeth(n); //or another number
}
Typo
  • 1,875
  • 1
  • 19
  • 32
  • No, that wouldn't work. Because you're in effect re-excuting `myMeth(n)`, and if the code marked as `do something` is not pure-functional (ie., it modifies the environment), you would end up modifying the env twice. – One Two Three Jan 16 '14 at 19:48
0

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);
    }
boxed__l
  • 1,334
  • 1
  • 10
  • 24
0
// 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.

user3100783
  • 259
  • 1
  • 8
-1

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.

Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
  • 2
    Can you actually provide the solution? Handling exceptions in recursive methods seems pretty reasonable to me. – OrangeDog Jan 16 '14 at 19:44
  • @OrangeDog, handling exception - yes, returning to the first recursive forking - isn't. It can be done, but it will miss greatly the purpose of recursive and the solution as i said will be hard to read and understand. – Orel Eraki Jan 16 '14 at 19:46