c#终结器抛出异常? [英] c# finalizer throwing exception?

查看:21
本文介绍了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#终结器抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆