Search code examples
javastack-overflow

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.


Solution

  • Seems to work:

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