title: Global handling exception in WPF app with Caliburn.Micro
body: <p>Hi I try implement solution from this site im my WPF app for global exception handling.</p>
  
  <p><a href="http://www.codeproject.com/Articles/90866/Unhandled-Exception-Handler-For-WPF-Applications.aspx" rel="noreferrer">http://www.codeproject.com/Articles/90866/Unhandled-Exception-Handler-For-WPF-Applications.aspx</a></p>
  
  <p>I use Caliburn Micro as MVVM framework. Service I have in external assembly and it is injected in view model class with MEF.</p>
  
  <p>Here is my implementation for global exception handling in WPF app.</p>
  
  <p>App.xaml</p>
  
  <pre><code>         DispatcherUnhandledException="Application_DispatcherUnhandledException"
           Startup="Application_Startup"
  </code></pre>
  
  <p>App class:</p>
  
  <pre><code>public partial class App : Application
  {
      private IMessageBox _msgBox = new MessageBoxes.MessageBoxes();
  
      public bool DoHandle { get; set; }
  
      private void Application_Startup(object sender, StartupEventArgs e)
      {
          AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
      }
  
      private void Application_DispatcherUnhandledException(object sender,
                             System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
      {
          if (DoHandle)
          {
              _msgBox.ShowException(e.Exception);
               e.Handled = true;
          }
          else
          {
              _msgBox.ShowException(e.Exception);
              e.Handled = false;
          }
      }
  
  
      void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
      {
          var ex = e.ExceptionObject as Exception;
          _msgBox.ShowException(ex);
      }
  
  
  }
  </code></pre>
  
  <p>Service method from external assembly:</p>
  
  <pre><code>    public void ServiceLogOn()
      {
          try
          {
  
          }
          catch (Exception ex)
          {
  
              throw ex;
          }
      }
  </code></pre>
  
  <p>This service method is call in view model class for example on button click event:</p>
  
  <pre><code>    [Export(typeof(ILogOnViewModel))]
      public class LogOnViewModel : Screen, ILogOnViewModel
      {
          public void LogOn()
          {
              _service.ServiceLogOn();
          }
      }
  </code></pre>
  
  <ol>
  <li><p>I run WPF app in Visual Studio and produce exception with message "Bad credentials" in ServiceLogOn method.</p>
  
  <p>I expect that I see the message box with the exception.</p>
  
  <p>But Visual Studio stop debuging app and show exception in service method in service project.</p></li>
  <li><p>So I try run WPF from exe file and produce same exception in ServiceLogOn method.</p>
  
  <p>I get this error:</p>
  
  <p>Exception has been throw by target of an invocation.</p></li>
  </ol>
  
  <p>Any exception from view model class is not handled in methods:</p>
  
  <ul>
  <li>Application_DispatcherUnhandledException</li>
  <li>or CurrentDomain_UnhandledException.</li>
  </ul>
  
  <p>in App class.</p>
  
  <p>What I do bad?</p>
  
  <p><strong>EDITED with Simon Fox’s answer.</strong></p>
  
  <p>I try implement in MEF bootstraper advice of Simon Fox’s, but I still something do wrong.
  I move handle logic for exception to OnUnhandledException method in bootstraper class.</p>
  
  <p>Here is my code from bootstraper class:</p>
  
  <pre><code> public class MefBootStrapper : Bootstrapper&lt;IShellViewModel&gt;
      { 
  //...
      private IMessageBox _msgBox = new MessageBoxes.MessageBoxes();
  
      public bool DoHandle { get; set; }
  
      protected override void OnUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
      {
          if (DoHandle)
          {
              _msgBox.ShowException(e.Exception);
              e.Handled = true;
          }
          else
          {
              _msgBox.ShowException(e.Exception);
              e.Handled = false;
          }
  
      }
  //...
      }
  </code></pre>
  
  <p>I bind some method from view model on button and throw new exception. Something like this:</p>
  
  <pre><code>public void LogOn()
  {
      throw new ArgumentException("Bad argument");
  }
  </code></pre>
  
  <p>But result is sam, I test app out of Visual Studio and get this exception.</p>
  
  <p>Exception has been throw by target of an invocation.</p>