Intercepting an exception inside IDisposable.Dispose

前端 未结 11 1863
有刺的猬
有刺的猬 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:30

    Instead of the syntactic sugar of the using statement, why not just implement your own logic for this. Something like:

    try
    {
      MyWrapper wrapper = new MyWrapper();
    
    }
    catch (Exception e)
    {
      wrapper.CaughtException = true;
    }
    finally
    {
       if (wrapper != null)
       {
          wrapper.Dispose();
       }
    }
    

提交回复
热议问题