try/catch on stack overflows in java?

stereos picture stereos · Mar 29, 2010 · Viewed 35.2k times · Source

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.

Answer

Thilo picture Thilo · Mar 29, 2010

Seems to work:

public class Test {

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

}