// 第 12 周 – 递归的深度是有限的：StackOverflowError
// 运行：java StackDepth.java
public class StackDepth {
    static int depth = 0;

    static void infinite() {   // 没有基本情况！
        depth++;
        infinite();
    }

    static long sumTo(long n) { return n == 0 ? 0 : n + sumTo(n - 1); }

    public static void main(String[] args) {
        try {
            infinite();
        } catch (StackOverflowError e) {
            System.out.println("StackOverflowError at a call depth of " + depth);
        }
        System.out.println("sumTo(5000) = " + sumTo(5000));
        try {
            System.out.println("sumTo(1 000 000) = " + sumTo(1_000_000));
        } catch (StackOverflowError e) {
            System.out.println("sumTo(1 000 000): StackOverflowError – iteration is needed here!");
        }
    }
}
