Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why can you even 'catch' StackOverflowError – how is it made to work?

I found this code block in the Javagony article on esolangs.org:

…In Javagony, the most common way to replicate loops is through recursion. If your loop has too many steps, you will get a stack overflow error, however, this can just be caught. So, to create a loop, we can start with a function that calls itself, and a try catch.

public void loop() {
   try {
       loop();
   catch (StackOverflowException /* sic */ e) {
       loop();
   }
}

Alright, so I know you should not "catch" the StackOverflowError, as many questions asked before me has already concluded; but I just cannot help get curious about what could happen if you just try to catch it? Will it grant you infinite recursions or will the JVM just breakdown and explode?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Anyway, here’s what I have written:

public class Javagony
{
    public static void main(String[] args)
    {
        loop();
    }

    static int n = 1;

    public static void loop()
    {
        System.out.println(n);
        n++;
        try
        {
            loop();
        }
        catch (StackOverflowError _e)
        {
            loop();
        }
    }
}

I then ran it with java Javagony.java. It had been a while since then, and the numbers just keep scrolling. It has now reached 4072342 (in comparison, the same programme without try catch block dies on ~40000) and is still going on. But how could this even happen? Doesn’t StackOverflowError indicate that the resources have been exhausted, and the maximum capacity of function calls has been reached already? Why simply catching and ignoring it keeps the recursion going on?

Ps. Process has been interrupted, please don’t worry about that

>Solution :

Alright, so I know you should not "catch" the StackOverflowError, as many questions asked before me has already concluded;

Generally speaking, you should not catch it, because it’s not ordinarily something you can recover from. You can’t, generally, even rely on wrapping and rethrowing it.

but I just cannot help get curious about what could happen if you just try to catch it? Will it grant you infinite recursions

No.

or will the JVM just breakdown and explode?

No.

When there is no room on the stack, you cannot successfully invoke any methods or constructors. That’s pretty crippling, and generally speaking, if you try to recover from it then you’re likely to just get another StackOverflowException. However, when a method terminates, including by throwing a StackOverflowException, a little stack space is freed up, so you might then be able to do a little something.

Now consider your particular example. At the bottom of the recursion, some invocation of loop() throws a SOE. The parent invocation catches it, and in the handler tries recursing again. There is every reason to expect that that also will throw SOE, which will escape to the next level up. But now that recursive call handles the exception, and it has enough stack space to get in one more iteration. When that throws, it is handled one more level up, which now has space for two more iterations (each of which starts fresh with respect to exception handling). And so on …

That does not give you infinite recursion, but it does greatly magnify the overall number of successful executions of loop() you can get before an SOE finally bubbles all the way out of the top-level call. I haven’t done a full analysis, but the number of executions you can expect is more than squared.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading