title: C# Exceptions only caught when debugging?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/2936947/exception-handling-problem-in-release-mode">Exception handling problem in release mode</a>  </p>
  </blockquote>
  
  
  
  <p>I suspect there is a perfectly simple explanation for this, but I can't seem to find it.</p>
  
  <p>When my WinForms C# 4.0 application loads itself in the Program.cs file, the entire Main() function has inside it a try/catch statement.</p>
  
  <p>I have written a little exception wrapper which behaves quite similarly to the vanilla .net "uncaught exception" box, except it provides a bit more information, allows the exception tree to be saved (serialised), and it allows the user to submit the error report directly to me.</p>
  
  <p>Now, it works fine while debugging (F5). If I trigger an exception <em>anywhere</em> in the program which is in the main thread, if there is not try/catch then the exception fires its way all the way back to Main() and shows the custom window.</p>
  
  <p>(All other exceptions I have accounted for and are handled appropriately).</p>
  
  <p>When I run the program simply by running the .exe file, the vanilla .net exception box comes up, not the one I have coded.</p>
  
  <p>Is there any reason you can think of why this would happen?
  The strangest thing is that it behaves quite differently when running in debug mode vs running on its own.
  I am building as debug - not release.</p>
  
  <p>Edit (22-March-11):</p>
  
  <p>I'm just adding a little addendum here, in case some of you can't find the answer hidden in the comments for the accepted answer below:
  Forget that I said I was building as debug instead of release. That is of no relevance - I just added it for extra info. What is important is that when I'm debug<i>ging</i> in VS the exceptions are caught as expected, but when executing my EXE outside VS they aren't.</p>
  
  <p>As Cody said, <code>Application.Run()</code> has its own handler for exceptions which is why they never get to my main <code>catch</code>, however I mentioned that I am not even using <code>Application.Run()</code> anywhere in my code... instead my GUI is first launched with <code>Form.ShowDialog()</code>.</p>
  
  <p>I have done some experimentation, and can confirm that <code>Form.ShowDialog()</code> behaves the same was as <code>Application.Run()</code> in that exceptions are handled within the method itself.</p>