【发布时间】:2012-04-11 18:41:53
【问题描述】:
我有这门课:
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 包装它 - 我会NEVER 知道有错误
3) what 我应该在这里做吗?
编辑
基于"if I **forget** to call the Dispose method - so the GC will do it eventually.... it is better later then never..." 的MSDN 模式。所以我的问题是特别是关于 Finilize(析构函数)中的异常
【问题讨论】:
-
我会称它为“Finalize method”,而不是“析构函数”,尽管它使用析构函数语法。
-
@UweKeim - C# 引用称它们为析构函数。
标签: c# exception .net-4.0 garbage-collection destructor