title: Catching exceptions in Dispose during an exception
body: <p>Imagine that in C# (in .NET 4.5.x) that you have a <code>using</code> block around an object which implements <code>IDisposible</code>.  Imagine that</p>
  
  <ol>
  <li>an exception is thrown within the using block</li>
  <li>when the dispose method is called, an additional exception is thrown</li>
  </ol>
  
  <p>What is the best way to pass the two exceptions to a global error handler?</p>
  
  <p>Code Sample:</p>
  
  <pre><code>class SampleClass : IDisposable
  {
      void doSomething()
      {
          // Imagine that this code is actually doing something
          // Except that it unexpectedly hits an exception
          throw new Exception("Exception A");
      }
  
      void Dispose()
      {
         // Imagine that this code is doing some cleanup
         // Except that it is buggy
         // And throws more exceptions
         throw new Exception("Exception B");
      }
  }
  
  static void Main()
  {
      try
      {
          using(var c = new SampleClass())
          {
              c.doSomething();
          }
      } 
      catch (Exception e)
      {
          // Code that records and reports exceptions
          RecordException(e);
      } 
  }
  </code></pre>
  
  <p>In the above example, I want all the details of both <code>Exception A</code> and <code>Exception B</code> recorded.  What is the best way to pass the two exceptions to <code>RecordException</code>?</p>
  
  <p><a href="http://www.digitallycreated.net/Blog/51/c%23-using-blocks-can-swallow-exceptions" rel="nofollow">This article</a> proposes a <code>SafeUsingBlock</code> extension.  Is that the established best practice?</p>
  
  <p><strong>Edit:</strong> In this case I control and can re-write <code>SampleClass</code> and <code>SampleClass.Dispose()</code></p>