views:

135

answers:

2

I have a SCSF application i am trying to handle most of the exceptions using

Application.ThreadException += new ThreadExceptionEventHandler(new ThreadExceptionHandler().ApplicationThreadException);

The event handler :-

public class ThreadExceptionHandler
    {
        public void ApplicationThreadException(object sender, ThreadExceptionEventArgs e)
        {
            MessageBox.Show(e.Exception.Message, "An exception occurred:", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

Works fine . I can catch all the application exceptions in this block.

But the problem is after handling the exception the code again goes and executes the same exception generating code again. This happens till the time I get a windows message windows to send the error info to microsoft.

Could any one please help in telling me where I might be going wrong.

Thanks in Advance

Vikram

Note :- Currently i am throwing New Exception("Test Exception"); from a button event. I am doing this to provide event handling in my application.

+1  A: 

You have to set

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

See this MSDN page for example code

But note that this kind of catch-all exception handling is not a good replacement for exception handling inside your logic. It is a good backup, but the best thing to do in a global handler is to log the information and exit. Your app could be in an unsafe/undefined state.

Henk Holterman
Thanks for the help :)In my main code block for test i have put both appdomain AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(AppDomainUnhandledException);and Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);But now i find that thread exception event is not getting fired.I have also set Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Vikram I Code
A: 

After some banging my head against the code I found that the problem was due to the fact that my SCSF solution had a winforms Shell and on that shell there were WPF usercontrols.

When the exception where generated on these WPF usercontrol (mostly the case) they are not caught by Application.ThreadException coz Application class for WPF is different than that for Winforms.

In WPF application one need to handle Application.DispacherUnhandledException event.

Just my little finding ...

Vikram I Code