
我已经编写代码来尝试捕获StackOverFlowExceptions:
try { recursiveFxn(100000); }catch(Exception){} private voID recursiveFxn(int countdown){if (countdown > 0) recursiveFxn(countdown - 1);else throw new Exception("lol. Forced exception.");} 但是我仍然遇到程序崩溃(在NUnit和我正在运行的网页).为什么没有捕获异常?
解决方法 由于.NET Framework 2.0,StackOverflowException不能被捕获.这是因为它被认为是不好的做法.引用 MSDN documentation:Starting with the .NET Framework
version 2.0,aStackOverflowException
object cannot be caught by a try-catch
block and the corresponding process is
terminated by default. Consequently,
users are advised to write their code
to detect and prevent a stack
overflow. For example,if your
application depends on recursion,use
a counter or a state condition to
terminate the recursive loop.
现在,捕获StackOverflowException的唯一方法是当用户代码抛出时,如blog by Jared Parsons所述.除了hosting the CLR之外,您可以处理(但不是捕获)StackOverflowException,并设计出一种方法来执行你的程序继续
请注意,由于堆栈在发生异常时展开,在2.0版本的.Net中,当处理StackOverflowException时,堆栈实际上会更短,这样可以在不生成另一个StackOverflowException的情况下执行此 *** 作.
总结以上是内存溢出为你收集整理的C# – 如何处理/捕获StackOverFlowExceptions?全部内容,希望文章能够帮你解决C# – 如何处理/捕获StackOverFlowExceptions?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)