c# 溢出抛异常_C#捕获堆栈溢出异常

正确的方法是修复溢出,但是.

你可以给自己一个更大的一叠:-using System.Threading;Thread T = new Thread(threadDelegate, stackSizeInBytes);T.Start();

您可以使用System.Diagnostis.StackTrace FrameCount属性来计数您使用过的帧,并在达到框架限制时抛出您自己的异常。

或者,您可以计算剩余堆栈的大小,并在堆栈低于阈值时抛出自己的异常:-class Program{

static int n;

static int topOfStack;

const int stackSize = 1000000; // Default?

// The func is 76 bytes, but we need space to unwind the exception.

const int spaceRequired = 18*1024;

unsafe static void Main(string[] args)

{

int var;

topOfStack = (int)&var;

n=0;

recurse();

}

unsafe static void recurse()

{

int remaining;

remaining = stackSize - (topOfStack - (int)&remaining);

if (remaining 

throw new Exception("Cheese");

n++;

recurse();

}}

抓住奶酪就行了。;)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值