Friend Class ThreadExceptionHandler Public Sub Application_ThreadException(ByVal sender As System.Object, ByVal e As ThreadExceptionEventArgs) 'Display Message for the user that the program is going south 'Log the error Dim ex As Exception = e.Exception c = e.Exception.Message c = c & vbCrLf & "sender : " & sender.ToString c = c & vbCrLf & Environment.GetEnvironmentVariable("username") & " op " & My.Computer.Name c = c & vbCrLf c = c & vbCrLf & ex.ToString ' send myself an email with c in the boddy Application.Exit() End Sub End Class ' ThreadExceptionHandlerThis class is invoked as follows
Dim handler As ThreadExceptionHandler = _ New ThreadExceptionHandler() AddHandler Application.ThreadException, _ AddressOf handler.Application_ThreadExceptionThis approach works very well for most of the crucial errors, but for conversion errors that can happen anywhere and are difficult to reproduce, the information one gets is a bit sketchy.