Intercepting an exception inside IDisposable.Dispose

前端 未结 11 1823
有刺的猬
有刺的猬 2020-12-13 02:07

In the IDisposable.Dispose method is there a way to figure out if an exception is being thrown?

using (MyWrapper wrapper = new MyWrapper())
{
           


        
11条回答
  •  攒了一身酷
    2020-12-13 02:37

    No, there is no way to do this in the .Net framework, you cannot figure out the current-exception-which-is-being-thrown in a finally clause.

    See this post on my blog, for a comparison with a similar pattern in Ruby, it highlights the gaps I think exist with the IDisposable pattern.

    Ayende has a trick that will allow you to detect an exception happened, however, it will not tell you which exception it was.

提交回复
热议问题