System.StackOverflowException C#

问题描述

如何绕过stackoverflow异常,或者有更好的方法呢?任何帮助将不胜感激!

public MetricItemDetail GetData(int itemId,int itemTwoId)
{

        //some of my code goes here..

         try
                {
                    return new Class
                    {
                        Value = GetItemDetails(itemId,itemIdTwo)

                    };
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0} Exception caught.",e);
                    return new Class();
                }

}

public double GetItemDetails(int itemId,int itemTwoId)
{

   var currentValue = GetData(itemId,itemTwoId); // The problem is here..this is where I get the stackoverflow exception

}

解决方法

GetItemDetails和GetData方法相互调用,它会导致每次调用具有堆栈分配的“无限”循环,不幸的是堆栈大小不是无限的;)

在此处查看更多详细信息:https://www.dotnetperls.com/stackoverflowexception

StackOverflowException无法被粘贴,无法被捕获,必须避免。它的存在是为了保护.NET运行时免受致命崩溃的影响。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...