我知道无法捕获.NET中的StackOverflowExceptions,删除它们的进程,并且没有堆栈跟踪.这在MSDN上正式记录.但是,我想知道这种行为背后的技术(或其他)原因是什么.所有MSDN都说:
在.NET Framework的早期版本中,您的应用程序可以捕获StackOverflowException对象(例如,从无限递归中恢复).但是,目前不鼓励这种做法,因为需要大量额外的代码来可靠地捕获堆栈溢出异常并继续执行程序.
什么是"重要的附加代码"?这种行为还有其他记录的原因吗?即使我们无法捕获SOE,为什么我们至少不能获得堆栈跟踪?几个同事和我只是沉没几个小时来调试生产StackOverflowException,这可能需要几分钟的堆栈跟踪,所以我想知道我是否有充分的理由让我受苦.
请考虑以下代码.
public class Action {
private static int i=1;
public static void main(String[] args) {
try{
System.out.println(i);
i++;
main(args);
}catch (StackOverflowError e){
System.out.println(i);
i++;
main(args);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到了4338正确的价值.捕获StackOverflowError输出后如下连线.
4336
4337
4338 // up to this point out put can understand
433943394339 // 4339 repeating thrice
434043404340
4341
434243424342
434343434343
4344
4345
434643464346
434743474347
4348
434943494349
435043504350
Run Code Online (Sandbox Code Playgroud)
在这里考虑现场演示.它正常工作i=4330.其实这是怎么发生的?
供参考:
我做了以下代码来实现这里发生的事情.
public class Action {
private static int i = 1;
private static …Run Code Online (Sandbox Code Playgroud) 有这个代码:
public class Main {
public static void main(final String[] args) throws Exception {
System.out.print("1");
doAnything();
System.out.println("2");
}
private static void doAnything() {
try {
doAnything();
} catch (final Error e) {
System.out.print("y");
}
}
}
Run Code Online (Sandbox Code Playgroud)
还有输出:
1yyyyyyyy2
Run Code Online (Sandbox Code Playgroud)
为什么它打印"y"八次而不再打印.遇到Java println()时如何调用StackOverflowError?