Sor*_*ter 2 java stack-overflow exception
下面的程序正确编译。是什么导致堆栈溢出错误?如何将异常推入堆栈?
public class Reluctant {
private Reluctant internalInstance = new Reluctant();
public Reluctant() throws Exception {
throw new Exception("I’m not coming out");
}
public static void main(String[] args) {
try {
Reluctant b = new Reluctant();
System.out.println("Surprise!");
} catch (Exception ex) {
System.out.println("I told you so");
}
}
}
Run Code Online (Sandbox Code Playgroud)
方法中的这一行会main导致无限递归:
Reluctant b = new Reluctant();
Run Code Online (Sandbox Code Playgroud)
每次尝试创建的实例时Reluctant,要做的第一件事就是创建另一个实例,该实例创建另一个实例,再创建另一个实例,然后...您就明白了。
瞧,堆栈溢出!
| 归档时间: |
|
| 查看次数: |
97 次 |
| 最近记录: |