title: await in try-finally block
body: <p>I've been playing around with the Visual Studio 14 CTP 2. This version of C# vNext enables the use of the <code>await</code> keyword inside a finally block.</p>
  
  <p>I am trying to figure out how this was implemented. I know this is an <strong>implementation detail</strong> and is subject to change until the RTM release, but i still had to pick my brain with this feature.</p>
  
  <p>To try and understand the underlying compiler generated code, i created this sample code:</p>
  
  <pre><code>private async void button1_Click(object sender, EventArgs e)
  {
      try
      {
      }
      finally
      {
          await MyFinallyTest();
      }
  }
  
  private async Task MyFinallyTest()
  {
      await Task.Delay(1000);
  }
  </code></pre>
  
  <p>This is the compiler generated class:</p>
  
  <pre><code>[CompilerGenerated]
  private sealed class &lt;button1_Click&gt;d__1 : IAsyncStateMachine
  {
      public int &lt;&gt;1__state;
      public Form1 &lt;&gt;4__this;
      public object &lt;&gt;7__wrap1;
      public int &lt;&gt;7__wrap2;
      public AsyncVoidMethodBuilder &lt;&gt;t__builder;
      public TaskAwaiter &lt;&gt;u__$awaiter0;
  
      private void MoveNext()
      {
          int num = this.&lt;&gt;1__state;
          try
          {
              TaskAwaiter awaiter;
              switch (num)
              {
                  case 1:
                      break;
  
                  default:
                  {
                      this.&lt;&gt;7__wrap1 = null;
                      this.&lt;&gt;7__wrap2 = 0;
  
                      try
                      {
                      }
                      catch (object obj2)
                      {
                          this.&lt;&gt;7__wrap1 = obj2;
                      }
  
                      awaiter = this.&lt;&gt;4__this.MyFinallyTest().GetAwaiter();
                      if (awaiter.IsCompleted)
                      {
                          goto Label_0096;
                      }
  
                      this.&lt;&gt;1__state = num = 1;
                      this.&lt;&gt;u__$awaiter0 = awaiter;
  
                      Form1.&lt;button1_Click&gt;d__1 stateMachine = this;
                      this.&lt;&gt;t__builder.AwaitUnsafeOnCompleted&lt;TaskAwaiter, Form1.&lt;button1_Click&gt;d__1&gt;(ref awaiter, ref stateMachine);
                      return;
                  }
              }
  
              awaiter = this.&lt;&gt;u__$awaiter0;
              this.&lt;&gt;u__$awaiter0 = new TaskAwaiter();
              this.&lt;&gt;1__state = num = -1;
  
          Label_0096:
              awaiter.GetResult();
              awaiter = new TaskAwaiter();
              object obj3 = this.&lt;&gt;7__wrap1;
              if (obj3 != null)
              {
                  Exception source = obj3 as Exception;
                  if (source &lt;= null)
                  {
                      throw obj3;
                  }
                  ExceptionDispatchInfo.Capture(source).Throw();
              }
  
              int num1 = this.&lt;&gt;7__wrap2;
              this.&lt;&gt;7__wrap1 = null;
          }
          catch (Exception exception2)
          {
              this.&lt;&gt;1__state = -2;
              this.&lt;&gt;t__builder.SetException(exception2);
              return;
          }
          this.&lt;&gt;1__state = -2;
          this.&lt;&gt;t__builder.SetResult();
      }
  
      [DebuggerHidden]
      private void SetStateMachine(IAsyncStateMachine stateMachine)
      {
      }
  }
  </code></pre>
  
  <p>From what i understand, the compiler is taking the <code>finally</code> code block and moving it to after the compiler generated <code>catch</code> block. Something similar to what we had to do if we wanted to <code>await</code> something in a catch-finally up untill C# 6.0.</p>
  
  <p>I see a couple of things i dont understand:</p>
  
  <ol>
  <li><p>The compiler is adding a generated <code>catch</code> block (which wasn't
  present in my method) in the form of <code>catch (object obj2)</code> and
  setting its internal <code>object</code> to the obj2 exception. I dont understand why this is being done.</p></li>
  <li><p>The <code>finally</code> block which i created no longer exist. Does that mean
  that any code which is <code>awaited</code> inside a <code>finally</code> block doesn't
  get to "enjoy" the guarantees we get from actually putting code
  inside such a block?</p></li>
  </ol>