Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

try/catch on stack overflows in java?

Can you try/catch a stack overflow exception in java? It seems to be throwing itself either way. When my procedures overflows, I'd like to "penalize" that value.

like image 269
stereos Avatar asked Mar 29 '10 03:03

stereos


People also ask

Can you try catch a stack overflow?

Starting with the . NET Framework 2.0, you can't catch a StackOverflowException object with a try / catch block, and the corresponding process is terminated by default. Consequently, you should write your code to detect and prevent a stack overflow.

How does Java handle stack overflow?

Increase Thread Stack Size (-Xss) Increasing the stack size can be useful, for example, when the program involves calling a large number of methods or using lots of local variables. This will set the thread's stack size to 4 mb which should prevent the JVM from throwing a java.

Can we handle stack overflow in Java?

Stack overflow means, that you have no room to store local variables and return adresses. If your jvm does some form of compiling, you have the stackoverflow in the jvm as well and that means, you can't handle it or catch it. The jvm has to terminate.


4 Answers

Seems to work:

public class Test {

    public static void main(String[] argv){
        try{
            main(null);
        }
        catch(StackOverflowError e){
            System.err.println("ouch!");
        }
    }

}
like image 87
Thilo Avatar answered Oct 18 '22 23:10

Thilo


If you are getting a stack overflow, you are likely attempting infinite recursion or are severely abusing function invocations. Perhaps you might consider making some of your procedures iterative instead of recursive or double-check that you have a correct base case in your recursive procedure. Catching a stack overflow exception is a bad idea; you are treating the symptoms without addressing the underlying cause.

like image 25
Michael Aaron Safyan Avatar answered Oct 18 '22 22:10

Michael Aaron Safyan


You have to catch an Error, not the Exception

like image 4
Daniel Avatar answered Oct 18 '22 22:10

Daniel


The functional features of Java 8 makes this question incomparably more important. For while we start to use recursion massively, StackOverflowException is something we MUST count for.

The Java 8 lambdas types has no one among them that throws StackOverflowException. So, we have to create such. It is absolutely necessary, without that we won't pass even the IDE control.

For example, Integer -> Integer function type could look as:

@FunctionalInterface
public interface SoFunction <U> {
    public U apply(Integer index) throws StackOverflowException;
}

After that we can write a function that will accept lambdas throwing StackOverflowException.

public T get(int currentIndex) throws StackOverflowException{

And only now we can create a recursive lambda:

fiboSequence.setSequenceFunction(
            (i) ->
            fiboSequence.get(i-2).add(fiboSequence.get(i-1))
);

After that we can call the recursive chain fiboSequence.get(i)and get a result or a StackOverflowException if the whole chain was incomputable.

In the case of use of recursion SO gets absolutely different meaning: you have jumped too deep, repeat it dividing in more shallow steps.

like image 4
Gangnus Avatar answered Oct 19 '22 00:10

Gangnus