在 C# 中捕获 StackOverflow 异常的通用方法是什么?
如果我有一个我知道可能无限递归的方法,但我无法可靠地预测什么条件/参数会导致它,那么在 C# 中执行此操作的好方法是什么:
try
{
PotentiallyInfiniteRecursiveMethod();
}
catch (StackOverflowException)
{
// Handle gracefully.
}
显然在主线程中你不能执行此操作,但我曾多次被告知可以使用线程或 AppDomain 来完成此操作,但我从未见过有效的示例。有人知道这是如何可靠地完成的吗?
If I have a method that I know could potentially recurse infinitely, but I can't reliably predict what conditions/parameters would cause it, what's a good way in C# of doing this:
try
{
PotentiallyInfiniteRecursiveMethod();
}
catch (StackOverflowException)
{
// Handle gracefully.
}
Obviously in the main thread you can't do this, but I've been told a few times it's possible to do it using threads or AppDomain's, but I've never seen a working example. Anybody know how this is done reliably?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能。来自 MSDN
You can't. From MSDN
无法捕获 StackOverflowException,但您可以对未处理的异常执行某些操作:
There is no way to catch StackOverflowException, but you can do something with unhandled exception: