Why does this method print 4?

前端 未结 7 1638
抹茶落季
抹茶落季 2020-12-12 13:18

I was wondering what happens when you try to catch an StackOverflowError and came up with the following method:

class RandomNumberGenerator {

    static int         


        
相关标签:
7条回答
  • 2020-12-12 14:09

    This is not exactly an answer to the question but I just wanted to add something to the original question that I came across and how I understood the problem:

    In the original problem the exception is caught where it was possible:

    For example with jdk 1.7 it is caught at first place of occurence.

    but in earlier versions of jdk it looks like the exception is not being caught at the first place of occurence hence 4, 50 etc..

    Now if you remove the try catch block as following

    public static void main( String[] args ){
        System.out.println(cnt++);
        main(args);
    }
    

    Then you will see all the values of cnt ant the thrown exceptions (on jdk 1.7).

    I used netbeans to see the output, as the cmd will not show all the output and exception thrown.

    0 讨论(0)
提交回复
热议问题