title: Trapping exceptions for logging in a C++ CLI app
body: <p>I'm trying to trap any and all exceptions in a C++/CLI app so that I can log and record them (including a stack trace). So far I have some code which looked promising:</p>
  
  <pre><code>[STAThreadAttribute]
  int main(array&lt;System::String ^&gt; ^args)
  {
      // Enabling Windows XP visual effects before any controls are created
      Application::EnableVisualStyles();
      Application::SetCompatibleTextRenderingDefault(false); 
  
      // Create the main window and run it
      try
      {
          Application::Run(gcnew MainForm());
      }
      catch( System::Exception^ e )
      {
          String^ message = gcnew String("");
          System::Exception^ exceptionRecursor = e;
  
          message = "ERROR: Uncaught exception encountered!\n\n";
          while( exceptionRecursor )
          {
              message += exceptionRecursor-&gt;Source+"\n\t";
              message += exceptionRecursor-&gt;Message+"\n\t";
              message += exceptionRecursor-&gt;StackTrace+"\n\n";
              exceptionRecursor = exceptionRecursor-&gt;InnerException;
          }
          MessageBox::Show(message);
      }
  
      return 0;
  }
  </code></pre>
  
  <p>...but instead of disaplying a dialog box with my tidied up errors, I get something else:</p>
  
  <pre><code>An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
  
  Additional information: Exception has been thrown by the target of an invocation.
  </code></pre>
  
  <p>Is this because the <code>Run</code> command is trying to deal with the exception in some way? Do I need to handle things inside <code>MainForm</code> somewhere? ...or is there some other (better) way of going about this.</p>
  
  <p>Forgetting the source of the error for a moment (I'm mid development cycle and still debugging), it would be nice to be able to trap these errors and produce a neat little stack trace which could remain in the code right up to deployment and let users us know when things are going wrong. Eventually I'd wrap the error report into something that could report over the web.</p>