检测finally块中的异常

Pau*_*ner 6 .net exception-handling

我正在尝试编写一种Dispose具有抛出异常的潜力的方法.

try-finally通过using声明以一种模式调用dispose :

using(var widget = new Widget())
{
   widget.DoYourThing();
}
Run Code Online (Sandbox Code Playgroud)

问题是如果Dispose方法引发异常,它将替换在using块体内可能引发的任何异常.通常,此异常不如在体内抛出的异常有用.

我想要的是以Dispose这样的方式编写方法,即如果正在进行中的异常,它会吞下自己的异常.像下面这样的东西是理想的:

protected virtual void Dispose(bool disposing)
{
    try
    {
        this.Shutdown();
    }
    catch(Exception)
    {
        this.Abort();

        // Rethrow the exception if there is not one already in progress.
        if(!Runtime.IsHandlingException)
        {
            throw;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有什么能提供这些信息吗?