Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Exceptions only caught when debugging? [duplicate]

Possible Duplicate:
Exception handling problem in release mode

I suspect there is a perfectly simple explanation for this, but I can't seem to find it.

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.

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.

Now, it works fine while debugging (F5). If I trigger an exception anywhere 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.

(All other exceptions I have accounted for and are handled appropriately).

When I run the program simply by running the .exe file, the vanilla .net exception box comes up, not the one I have coded.

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.

Edit (22-March-11):

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 debugging in VS the exceptions are caught as expected, but when executing my EXE outside VS they aren't.

As Cody said, Application.Run() has its own handler for exceptions which is why they never get to my main catch, however I mentioned that I am not even using Application.Run() anywhere in my code... instead my GUI is first launched with Form.ShowDialog().

I have done some experimentation, and can confirm that Form.ShowDialog() behaves the same was as Application.Run() in that exceptions are handled within the method itself.

like image 985
Ozzah Avatar asked Feb 25 '11 04:02

Ozzah


1 Answers

This is the expected behavior.

The difference you see is a result of the application being run with the debugger attached. When you launch it from within Visual Studio, the debugger is automatically attached (unless, of course, you choose to "Start Without Debugging"). That disables the built-in exception handler, which is the one responsible for showing you the "vanilla" .NET exception dialog. Launching it from outside VS doesn't attach the debugger, leaving the built-in exception handling enabled. (Note that this has nothing to do with compiling your program in "Debug" mode versus "Release" mode.)

See the accepted answer to this related question for more information. I do not believe the difference between VB.NET and C# is relevant in this case.

As that answer mentions, there is a way to disable the built-in exception handler. But before choosing to do so, I recommend reconsidering your approach. Rather than wrapping your entire Main method in a try-catch block, which sounds like a bit of a code smell to me, you might consider handling the built-in AppDomain.UnhandledException event. Jeff Atwood posted a great article here on Code Project about how to replace the standard .NET exception handling with your own more user-friendly method. The solution he proposes has become only that much more elegant as later versions of the .NET FW have improved how the AppDomain.UnhandledException event is handled.

like image 121
Cody Gray Avatar answered Sep 22 '22 13:09

Cody Gray