为什么我不能处理这个溢出异常而不能做最终范围?
问题描述:
static void Main(string[] args)
{
try
{
Console.WriteLine("This is In Try");
Program p = new Program();
p.ExecuteOverflow(new Program());
}
catch (SystemException sex)
{
Console.WriteLine("This is catch with system exception : {0}", sex.ToString());
}
catch (Exception ex)
{
Console.WriteLine("This is in Catch with exception: {0}", ex.ToString());
}
finally
{
Console.WriteLine("In finally");
}
}
public void ExecuteOverflow(Program p)
{
p.ExecuteOverflow(new Program());
}
答
好点。
一旦出现溢出异常,堆栈上就没有空间来执行finally
块。 br />
因此,最后
未执行。
还有一些其他最终不执行的场景。
其中一个是ExecutingEngineException
。
事实上,有一些例外情况,finally
块的执行无法保证。
然而,溢出(以及其他这些)是错误,你的程序很可能无法恢复并将关闭。
因此,最终
未执行。
这个msdn链接 [ ^ ]将为您提供更多洞察力。
Good point.
Once there is an overflow exception, there is no space on the stack to execute afinally
block.
As a result,finally
is not executed.
There are a few other scenarios where finally does not execute.
One of them is theExecutingEngineException
.
Infact, there are a few exceptions where execution of thefinally
block is not guaranteed.
However, overflow (and these other others) are errors where in all probability your program cannot recover and will shutdown.
As a result, it makes some sense thatfinally
is not executed.
This msdn link[^] will give you little more insight.