Intercepting an exception inside IDisposable.Dispose

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

    You can do this buy implementing the Dispose method for the "MyWrapper" class. In the dispose method you can check to see if there is an exception as follows

    public void Dispose()
    {
        bool ExceptionOccurred = Marshal.GetExceptionPointers() != IntPtr.Zero
                                 || Marshal.GetExceptionCode() != 0;
        if(ExceptionOccurred)
        {
            System.Diagnostics.Debug.WriteLine("We had an exception");
        }
    }
    

提交回复
热议问题