title: Determine if executing in finally block due to exception being thrown
body: <p>Is it possible to determine if code is currently executing in the context of a <code>finally</code> handler as a result of an exception being thrown? I'm rather fond of using the <code>IDisposable</code> pattern to implement entry/exit scoping functionality, but one concern with this pattern is that you might not necessarily want the end-of-scope behavior to occur if an exception occurs in the body of the <code>using</code>. I'd be looking for something like this:</p>
  
  <pre><code>public static class MyClass
  {
      public static void MyMethod()
      {
          using (var scope = MyScopedBehavior.Begin())
          {
              //Do stuff with scope here
          }
      }
  }
  
  public sealed class MyScopedBehavior : IDisposable
  {
      private MyScopedBehavior()
      {
          //Start of scope behavior
      }
  
      public void Dispose()
      {
          //I only want to execute the following if we're not unwinding
          //through finally due to an exception:
          //...End of scope behavior    
      }
  
      public static MyScopedBehavior Begin()
      {
          return new MyScopedBehavior();
      }
  }
  </code></pre>
  
  <p>There are other ways I can accomplish this (pass a delegate to a function that surrounds the call with particular behavior), but I'm curious if it's possible to do it using the <code>IDisposable</code> pattern.</p>
  
  <hr>
  
  <p>Actually, this has apparently been asked and answered before <a href="https://stackoverflow.com/questions/1815492/how-to-determine-whether-a-net-exception-is-being-handled">here</a>. It's possible to detect in a very hackish sort of way. I wouldn't actually use that technique, but it's interesting to know that it's possible.</p>