[Solved] StackOverFlowError + Recursion funny trick [duplicate]


In the first example, let’s treat the whole function as if it doesn’t really call itself. Basically, a method call is made, and if the stack overflows, the same method call is made. Obviously, if the first call caused a stack overflow, nothing has changed and the second call causes a stack overflow, which is not caught and results in an exception display. Essentially, if the method it calls overflows the stack, then the number of method calls is finite. Now, because the method calls always calls itself, the method will overflow the stack at some point, and because of what we proved earlier, the number of method calls is finite.

Now that’s a nice application of the mathematical way of thinking to computer programming.

As for the second example, it is exactly the same, except because of the finally block, two stack overflows are tolerated before an exception report, and the same logic will lead to the same result.

solved StackOverFlowError + Recursion funny trick [duplicate]