微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

析构函数中的异常(c#)?

我有这门课:

public class TempFileRef
    {
        public readonly string FilePath;

        public TempFileRef(string filePath)
        {
            FilePath = filePath;
        }

        ~TempFileRef()
        {
            File.Delete(FilePath);    //<== what happens if exception ?
        }
    }

题 :

如果析构函数中存在异常会发生什么?

1)它会打破F-Queue中的其他终结吗?

2)我将用Try和Cache包装它 – 我永远不会知道有错误

3)我该怎么办?

编辑

它的MSDN模式基于“如果我忘记**调用dispose方法 – 那么GC最终会做到这一点……后来再好不过……”.所以我的问题是特别关于Finilize中的异常(析构函数)

解决方法

MSDN开始:

Exceptions that occur during destructor execution are worth special mention. If an exception occurs during destructor execution,and that exception is not caught,then the execution of that destructor is terminated and the destructor of the base class (if any) is called. If there is no base class (as in the case of the object type) or if there is no base class destructor,then the exception is discarded.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐