C# 终结器抛出异常?
引自 MSDN:
如果 Finalize 或 Finalize 的重写引发异常,运行时将忽略该异常,终止 Finalize 方法,并继续终结过程。
但如果我有:
~Person()
{
throw new Exception("meh");
}
那么它会导致运行时异常吗?
ps 我知道这永远不应该发生,但是我只是对这种行为感到好奇。我们的一个客户在所有终结器周围都有一个空的 try catch .. 当出现问题或重新设置对象时它甚至没有记录:/
Quote from MSDN:
If Finalize or an override of Finalize throws an exception, the runtime ignores the exception, terminates that Finalize method, and continues the finalization process.
Yet if I have:
~Person()
{
throw new Exception("meh");
}
then it results in a runtime exception?
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 :/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
链接引用的来源很重要。我必须假设它谈论的是旧版本的 .NET,也许是版本 1.x。它试图“容忍”未处理的异常,不发出吱吱声就吞掉它们。但效果并不好,无声地失败的代码块非常难以调试。
.NET 2.0 版本结束了这种情况,默认 CLR 主机会因任何未处理的异常而终止应用程序。终结器中的异常是致命的。
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.
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.
我很好奇 xamarin 中会发生什么,因为我在生产中看到过这种情况,并且 android 应用程序没有崩溃,终结器线程上可能发生锁定,并且应用程序运行低于标准,直到重新启动。
I'm curious as to what happens in xamarin since i've seen this happen in production and the android app did not crash, it is possible that a lock occurred on the finalizer thread and the app ran sub-par until restart.