title: How can I catch all exceptions, when I want to show a report and Crystal reports is not installed?
body: <p>I have a tool, which reads data from a database, modifies it and then displays it in a crystal reports report, which is shown in a separate window.</p>
  
  <p>The tool is sometimes used by people, who do not have Crystal Reports installed, so I want to display specific error message, which tells the user exactly what to do (instead of the generic message right now).</p>
  
  <p>I'm calling the window with the report like this:</p>
  
  <pre><code>try
  {
      ReportWindow report = new ReportWindow(resultList);
      report.Show();
  }
  catch (Exception ex)
  {
      this.LogAction("Could not open the report. To view it, you need to have Crystal Reports installed.", Notification.Error);
  }
  </code></pre>
  
  <p>(the catch branch is simplified for the sake of this question)</p>
  
  <p>ReportWindow.xaml.cs</p>
  
  <pre><code>public ReportWindow(List&lt;MeasureTestResult&gt; measurement)
  {
      this._measurementList = measurement;
      InitializeComponent();
  }
  </code></pre>
  
  <p>ReportWindow.xaml</p>
  
  <pre><code>&lt;Window x:Class="ResultFileViewer.Sources.ReportWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:viewer="clr-namespace:SAPBusinessObjects.WPF.Viewer;assembly=SAPBusinessObjects.WPF.Viewer"&gt;
      &lt;viewer:CrystalReportsViewer Name="CrystalReportsViewer1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" /&gt;    
  &lt;/Window&gt;
  </code></pre>
  
  <p>The line <code>InitializeComponent();</code> in the <code>ReportWindow</code> constructor throws several exceptions, like <code>XamlParseException</code> or an <code>InvalidCastException</code>. The problem is, that only the <code>XamlParseException</code> is caught by the catch. The other exceptions are only handled by my <code>DispatcherUnhandledException</code> property of my App.xaml.</p>
  
  <p>This is problematic because it either means that the whole application is shut down when e.Handled is set to false or a zombie process remains after the application is closed by the user, when e.Handled is set to true.</p>
  
  <p>How can I catch the currently unhandled exception in the class, that is calling the report window?</p>
  
  <p>I tried <a href="https://stackoverflow.com/questions/823929/can-i-handle-thrown-exceptions-in-xaml">this solution</a>, but that also didn't catch the <code>InvalidCastException</code>. </p>