Windows Forms
  A default error handler
  An Owner-drawn ListBox
  Call across thread boundaries

Get URL for this page

Adding an error handler for untrapped exceptions

In your application design you might want to register an exception handler for any exceptions you have not handled in your application. You can create an exception handler for your unhandled exceptions by calling the Application.AddOnThreadException method and passing in a reference to a method in your application. This method will then be called for any unhandled exceptions on that thread. The OnThreadException event is raised when the Windows Forms window procedure receives an exception.

Windows Forms provides a default exception handler and exception handling dialog named ThreadExceptionDialog. This dialog can be used during development, but you will want to supply your own custom exception handlers for your application when it is near completion.

The following example demonstrates how to create a very simple custom exception handler that displays the exception message and stack trace in a MessageBox:


' The Error Handler class
' We need a class because event handling methods can't be static
Friend Class CustomExceptionHandler

    ' Handle the exception event
    Public Sub OnThreadException(Sender As Object , t As ThreadExceptionEventArgs)

        Dim result As DialogResult = DialogResult.Cancel
        Try
            result = Me.ShowThreadExceptionDialog(t.Exception)
        Catch
            Try
                MessageBox.Show("Fatal Error", 
                                "Fatal Error", 
                                MessageBoxButtons.AbortRetryIgnore, 
                                MessageBoxIcon.Stop)
            Finally
                Application.Exit()
            End Try
        End Try

        If (result = DialogResult.Abort) Then
            Application.Exit()
        End If

    End Sub

    ' The simple dialog that is displayed when this class catches and exception
    Private Function ShowThreadExceptionDialog(ByVal e As Exception) As DialogResult
        Dim errorMsg As String = "An error occurred please contact the adminstrator with" & _
                                 " the following information:" & vbCrLf & vbCrLf
        errorMsg &= e.Message & vbCrLf & vbCrLf & "Stack Trace:" & _
                    vbCrLf & e.StackTrace
        Return MessageBox.Show(errorMsg, _
                                "Application Error", _
                                MessageBoxButtons.AbortRetryIgnore, _
                                MessageBoxIcon.Stop)
    End Function
End Class

....

' Register the custom error handler as soon as we can in Main
' to make sure that we catch as many exceptions as possible
Public Shared Sub Main()
    'Explicitly set apartment state to Single Thread Apartment (STA)
    System.Threading.Thread.CurrentThread.ApartmentState = System.Threading.ApartmentState.STA
    Dim eh As New CustomExceptionHandler()
    AddHandler Application.ThreadException, AddressOf eh.OnThreadException
    Application.Run(New ErrorHandler())
End Sub
VB



 
VB Error Handling Sample

[Run Sample] | [View Source]


Copyright 2001 Microsoft Corporation. All rights reserved.