【问题标题】:.NET Core - how to catch a StackOverflowException.NET Core - 如何捕获 StackOverflowException
【发布时间】:2016-09-14 17:13:07
【问题描述】:

完美的 StackOverflow 问题终于来了....

如何捕获 StackOverflow 异常!

似乎在 .NET Core 中 StackOverflowException 不可用:

如果我运行这段代码:

using System;

namespace PlayGround.Core.Console
{
    public class Program
    {
        public static void Main(string[] args)
        {
            try
            {
                DoSomething();
            }
            catch(Exception e)
            {
                System.Console.WriteLine("Bugger");
            }

        }

        private static void DoSomething()
        {
            DoSomething();
        }
    }
}

我明白了:

您可以看到我的异常处理程序没有运行。那么如何在 .NET Core 中捕获这个异常呢?

2017 年 9 月 15 日编辑: 在 .NET Core 2.0 中现在有一个 StackOverflowException 类,但它实际上仍然没有捕获 stackoverflow 异常。

【问题讨论】:

标签: exception exception-handling .net-core


【解决方案1】:

从 CLR 2.0 开始,您无法捕获 StackOverflowException。除非你是那个扔它的人。

https://blogs.msdn.microsoft.com/jaredpar/2008/10/22/when-can-you-catch-a-stackoverflowexception/

【讨论】:

  • 诸神一定是疯了
  • 很遗憾,您无法知道异常发生在代码的哪个位置。在大型应用程序的情况下,这是一个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-17
  • 2016-12-24
  • 2011-06-11
  • 2021-02-13
  • 2018-12-15
  • 2018-12-17
相关资源
最近更新 更多