Monday, 27 June 2005

Old blog - Application.ThreadException not firing in debug mode

Obligatory entry to avoid a whole month with no updates :-)

I've always used Application.ThreadException in windows forms apps to provide a last gasp exception handler (or in a lot of cases the only exception handler).

Simply do

AddHandler Application.ThreadException, AddressOf Me.HandleException

in the form load.

Then you provide a method such as

Public Sub HandleException(byval sender as object, byval args as System.Threading.ThreadExceptionEventArgs)

MessageBox.Show (args.Exception.ToString)

End Sub

The most annoying thing I always found about it was when debugging it never seemed to get invoked and VS.NET would show its unhandled exception dialog.

Turns out I've actually been doing it wrong for years. Instead of sticking it in form load put it in the constructor BEFORE the call to InitializeComponent. If you do that then HandleException gets invoked even when debugging.