加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 20:52:47 所属栏目:百科 来源:网络整理
导读:我有这门课: public class TempFileRef { public readonly string FilePath; public TempFileRef(string filePath) { FilePath = filePath; } ~TempFileRef() { File.Delete(FilePath); //== what happens if exception ? } } 题 : 如果析构函数中存在异常
我有这门课:

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读