Confusing output from infinite recursion within try-catch

前端 未结 7 2015
隐瞒了意图╮
隐瞒了意图╮ 2020-12-23 19:13

Consider the following code.

public class Action {
private static int i=1;
public static void main(String[] args)          


        
7条回答
  •  悲&欢浪女
    2020-12-23 20:10

    What I suspect being happening is this:

    1. Print i
    2. Print newline
    3. Increase i
    4. Enter main
    5. Print i
    6. Print newline
    7. Increase i
    8. Enter main
    9. Print i
    10. StackOverflow got thrown (instead of print newline)
    11. Return to main, now in the catch
    12. Print i
    13. StackOverflow got thrown again (instead of print newline)
    14. Return to main, in another catch body.
    15. Print i
    16. Print newline (now doesn't fail anymore, because we are two levels higher)
    17. Enter main, and go back to 1.

提交回复
热议问题