You can handle the Application.ThreadException event from the System.Windows.Forms namespace.
using System.Threading;
[STAThread]
public static void Main()
{
Application.ThreadException +=
new ThreadExceptionEventHandler( UnhandledExceptionCatcher );
Application.Run( new Form1() );
}
private static void UnhandledExceptionCatcher(object sender,
ThreadExceptionEventArgs e)
{
Console.WriteLine( "Caught an unhandled exception" );
}
See Application.ThreadException in the .NET Framework Class Library for a more detailed sample in both VB and C#.
Contributed from George Shepherd's Windows Forms FAQ