views:

1930

answers:

4

When working on ASP.NET 1.1 projects I always used the Global.asax to catch all errors. I'm looking for a similar way to catch all exceptions in a WinForms user control which ends up being a hosted IE control. What is the proper way to go about doing something like this?

+5  A: 

You need to handle the System.Windows.Forms.Application.ThreadException event for winforms. This article really helped me: http://bytes.com/forum/thread236199.html.

Chris Leon
+1  A: 

If you're using VB.NET, you can tap into the very convenient ApplicationEvents.vb. This file comes for free with a VB.NET WinForms project and contains a method for handling unhandled exceptions.

To get to this nifty file, it's "Project Properties >> Application >> Application Events"

If you're not using VB.NET, then yeah, it's handling Application.ThreadException.

Brad Tutterow
+1  A: 

Currently in my winforms app I have handlers for Application.ThreadException, as above, but also AppDomain.CurrentDomain.UnhandledException

Most exceptions arrive via the ThreadException handler, but the AppDomain one has also caught a few in my experience

Orion Edwards
+1  A: 

Also have a look at my question for some of the pitfalls (links to a couple of coding horror blog entries).

Ray