views:

132

answers:

2

I want to get Default Windows Forms Unhandled-Exception Dialog whenever my C# application encounters U-E. In vs 2005 when I turn off jit Debugging in app.conf like this:

<configuration>
   <system.windows.forms jitDebugging="false" />
<configuration>

the application behaves correctly and shows Windows Forms U-E default dialog, with Continue, Quit, call stack and all.

However in vs 2008, on the same machine or different, even though I diable jit I still get Default .NET Unhandled-Exception Dialog, with Debug, Send Report and Don't Send buttons.

How can I make my vs 2008 app act like the one I make in vs 2005, to show Windows Forms U-E dialog box?

Please do not recommend to use

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

just because I don't use custom handler in my vs 2005 project, why would I use in vs 2008? I want to let this job do CLR.

Any help is appreciated

+5  A: 

You are talking about different exception handling features. The ThreadExceptionDialog you see with the Quit and Continue buttons is triggered by the Application.ThreadException event. It will only ever appear if the exception happens on the UI thread, when an event handler that runs in response to a Windows message throws an exception. Any exception in a worker thread however will bomb the program through AppDomain.UnhandledException.

You cannot handle the latter, the program is dead and cannot continue. Displaying ThreadExceptionDialog is pointless.

You can make the program bomb consistently by disabling the ThreadException event by calling Application.SetUnhandledExceptionMode(). Now every unhandled exception will trigger AppDomain.UnhandledException and terminate the program. That's probably not what you want but is the wise choice.

Also note that the ThreadException event is disabled when you run the program with a debugger. Which is presumably why you see a difference in VS2008. There have otherwise not been any changes.

Hans Passant
Well, I have only UI thread in my app. Basically for instance I just created new sample app from scratch and added only nullreferenceexception in form constructor.I had tried to play with Application.SetUnhandledExceptionMode() without any difference.One more important note that I'm testing not on the machine with debugger but in the field pc, without such.My understanding is that VS2008 is adding some transparent threading mechanism preventing me to see what I want to see.
Michael
An exception in the form constructor will bomb through AppDomain.UnhandledException, the message loop hasn't been started yet. VS2008 or .NET 3.5 hasn't added anything transparent. Not sure what you want to see but logging the exception in an AppDomain.UnhandledException event handler is important to find out what went wrong. You really do have to do what you don't want recommended.
Hans Passant
you are right Hans, the error is non UI(UnhandledException) when thrown from constructor and it is main UI thread type(ThreadException) when thrown from any place within ui. So for the ThreadException I managed to get the ThreadExceptionDialog. Your answers helped a lot, pity I can't vote up now!
Michael
A: 

Answer by Hans Passant seems sensible. However, even though you say you don't want to I still recommend a handler on AppDomain.CurrentDomain.UnhandledException to make sure you determine what happens when your application crashes unexpectedly.

peSHIr