c# finalizer throwing exception?(c#终结器抛出异常?)
问题描述
引用自 MSDN:
如果 Finalize 或 Finalize 的覆盖引发异常,则运行时会忽略该异常,终止该 Finalize 方法,并继续终结过程.
If Finalize or an override of Finalize throws an exception, the runtime ignores the exception, terminates that Finalize method, and continues the finalization process.
如果我有:
~Person()
{
throw new Exception("meh");
}
那么它会导致运行时异常吗?
then it results in a runtime exception?
附言我知道这永远不会发生,但是我只是对这种行为感到好奇.我们的一个客户在他们所有的终结器周围都有一个空的 try catch.它甚至没有记录出现问题或重新定位对象:/
p.s. I know that this should never happen, however I'm just curious around this behaviour. One of our clients had an empty try catch around all of their finalizers.. it didn't even log when things went wrong or reserect the object :/
推荐答案
链接报价来源很重要.我不得不假设它谈论的是旧版本的 .NET,也许是 1.x 版.它试图容忍"未处理的异常,不吱声地吞下它们.效果并不好,大量代码静默失败非常难以调试.
Linking the source of your quote is important. I have to assume it talks about an old version of .NET, perhaps version 1.x. It tried to be "tolerant" of unhandled exceptions, swallowing them without a squeak. That did not work out well, chunks of code silently failing is extraordinarily hard to debug.
.NET 2.0 版本结束了这一点,默认 CLR 主机会因任何未处理的异常而终止应用程序.终结器中的异常是致命的.
The .NET 2.0 version put an end to that, the default CLR host terminates the app for any unhandled exception. An exception in a finalizer is fatal.
这篇关于c#终结器抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:c#终结器抛出异常?
基础教程推荐
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
