title: Exception handling inside "async void" WPF command handlers
body: <p>I'm reviewing some WPF code of my colleagues, which is a <strong>library</strong> of <code>UserControl</code>-based components with a lot of <strong><code>async void</code></strong> event and command handlers. These methods currently <strong>do not implement any error handling</strong> internally.</p>
  
  <p>The code in a nutshell:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Window.CommandBindings&gt;
      &lt;CommandBinding
          Command="ApplicationCommands.New"
          Executed="NewCommand_Executed"/&gt;
  &lt;/Window.CommandBindings&gt;
  </code></pre>
  
  <pre class="lang-cs prettyprint-override"><code>private async void NewCommand_Executed(object sender, ExecutedRoutedEventArgs e)
  {
      // do some fake async work (and may throw if timeout &lt; -1)
      var timeout = new Random(Environment.TickCount).Next(-100, 100);
      await Task.Delay(timeout);
  }
  </code></pre>
  
  <p>Exceptions thrown but not observed inside <code>NewCommand_Executed</code> <strong>can only be handled on a global level</strong> (e.g., with <code>AppDomain.CurrentDomain.UnhandledException</code>). Apparently, this is not a good idea.</p>
  
  <p><strong>I could handle exceptions locally:</strong></p>
  
  <pre class="lang-cs prettyprint-override"><code>private async void NewCommand_Executed(object sender, ExecutedRoutedEventArgs e)
  {
      try
      {
          // do some fake async work (throws if timeout &lt; -1)
          var timeout = new Random(Environment.TickCount).Next(-100, 100);
          await Task.Delay(timeout);
      }
      catch (Exception ex)
      {
          // somehow log and report the error
          MessageBox.Show(ex.Message);
      }
  }
  </code></pre>
  
  <p>However, in this case the host app's <strong>ViewModel would be unaware of errors</strong> inside <code>NewCommand_Executed</code>. Not an ideal solution either, plus the error reporting UI shouldn't always be a part of the library code.</p>
  
  <p><strong>Another approach is to handle them locally and fire a dedicated error event:</strong></p>
  
  <pre class="lang-cs prettyprint-override"><code>public class AsyncErrorEventArgs: EventArgs
  {
      public object Sender { get; internal set; }
      public ExecutedRoutedEventArgs Args { get; internal set; }
      public ExceptionDispatchInfo ExceptionInfo { get; internal set; }
  }
  
  public delegate void AsyncErrorEventHandler(object sender, AsyncErrorEventArgs e);
  
  public event AsyncErrorEventHandler AsyncErrorEvent;
  
  private async void NewCommand_Executed(object sender, ExecutedRoutedEventArgs e)
  {
      ExceptionDispatchInfo exceptionInfo = null;
  
      try
      {
          // do some fake async work (throws if timeout &lt; -1)
          var timeout = new Random(Environment.TickCount).Next(-100, 100);
          await Task.Delay(timeout);
      }
      catch (Exception ex)
      {
          // capture the error
          exceptionInfo = ExceptionDispatchInfo.Capture(ex);
      }
  
      if (exceptionInfo != null &amp;&amp; this.AsyncErrorEvent != null)
          this.AsyncErrorEvent(sender, new AsyncErrorEventArgs { 
              Sender = this, Args = e, ExceptionInfo = exceptionInfo });
  }
  </code></pre>
  
  <p>I like the last one the most, but I'd appreciate any other suggestions as my experience with WPF is somewhat limited.</p>
  
  <ul>
  <li><p><strong>Is there an established WPF pattern to propagate errors from <code>async void</code> command handlers to ViewModal?</strong></p></li>
  <li><p><strong>Is it generally a bad idea to do async work inside WPF command handlers</strong>, as perhaps they're intended for quick synchronous UI updates?</p></li>
  </ul>
  
  <p>I'm asking this question in the context of WPF, but I think it may as well apply to <code>async void</code> event handlers in WinForms.</p>