问题描述
http://en.wikipedia.org/wiki/comparison_of_C_Sharp_and_Java#Finally_Blocks_and_Uncaught_Exceptions 指出 finally
块并不总是运行.错了吧?
http://en.wikipedia.org/wiki/Comparison_of_C_Sharp_and_Java#Finally_Blocks_and_Uncaught_Exceptions states that the finally
block doesn't always run. That's wrong, right?
但是,我不需要捕获即可最终执行:
But, I don't need a catch to finally execute:
static void Main()
{
try { throw new Exception(); }
finally
{
Console.WriteLine("1");
}
}
推荐答案
我注意到没有人真正回答过您的问题,即此文本正确吗?"
I notice that no one has actually answered your question, which is "is this text correct?"
不,这是不正确的,因为它忽略了重要的一点.
No, it is not correct, in that it omits an important point.
CLI规范中未引用的相关部分是分区I的12.4.2节,其中指出:
The relevant portion of the CLI specification which it fails to quote is section 12.4.2 of Partition I, which states:
现在,正如其他人指出的那样,这里有一些微妙之处.请注意,该规范明确指出了当块退出时将最终执行.如果程序因故障快速终止,堆栈溢出或有人将电源线从墙上拔出而终止,则该块永远不会退出!该程序可以在该块退出之前 终止,因此最终将无法运行.
Now, as others have noted, there are some subtleties here. Notice that the specification clearly calls out that the finally is executed when the block exits. If the program is terminated by a failfast, by a stack overflow, or by someone pulling the power cord out of the wall, then the block never exits! The program could be terminated before the block exits, and therefore the finally would not run.
这篇关于最终执行时间的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!