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
?
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.