2

Is there a catchall handler I can register for the BeginInvoke method in C#?

On startup in my MainForm, I have this code

        Application.ThreadException += new ThreadExceptionEventHandler(UIExceptionHandler);
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
        AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);

I thought that was enough but an exception in another form is taking down my application. How do I deal with this? (I really don't want to go into every user control I have an add a proxy BeginInvoke method with my own try..catch nor do I want to add it to every BeginInvoke call I have....that would be quite tedious compared to adding a catchall handler).

thanks, Dean

Dean Hiller
  • 19,235
  • 25
  • 129
  • 212

1 Answers1

2

Try this:

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

Here is the MSDN on SetUnhandledExceptionMode

Also, here is a SO question that might make more sense if you are seeing this in debug (JoshL's answer). Basically, it defaults to automatic, and if the jitDebugging config is set to true (which it is if you are debugging), then the application will throw instead of catch into your handler.

Community
  • 1
  • 1
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180