1

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
  • You'll need to document what kind of code is throwing the exception. Nov 27, 2009 at 12:49

3 Answers 3

2

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.

1
  • 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); Nov 27, 2009 at 13:13
2

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 ...

0

you would be surprised by just handling the Application.DispatcherUnhandledException. I have worked with SCSF which had WPF user controls. Read through this post . http://social.msdn.microsoft.com/forums/en-US/wpf/thread/c57cac13-f960-49a1-94b5-a3fd316ac4bc/ i would recommend handling AppDomain.UnhandledException too.

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.