Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ThreadExceptionEventArgs Class

Provides data for the ThreadException event.

Namespace: System.Threading
Assembly: System (in system.dll)

'Declaration
Public Class ThreadExceptionEventArgs
	Inherits EventArgs
'Usage
Dim instance As ThreadExceptionEventArgs

public class ThreadExceptionEventArgs extends EventArgs
public class ThreadExceptionEventArgs extends EventArgs
Not applicable.

A ThreadExceptionEventArgs is created by a thread when an unhandled exception occurs. ThreadExceptionEventArgs contains the Exception that occurred.

The following example allows you to raise a ThreadException event by clicking button1 on a form. The example creates two classes. The ErrorHandler class creates the form and the button that raises the event. The CustomExceptionHandler class provides the methods to handle the exception.

In Main in the ErrorHandler class, the code creates a new instance of the exception handling class, that is, an instance of the CustomExceptionHandler. Then the instance is added to the event, and the application is run.

In the OnThreadException method in the CustomExceptionHandler class, the example uses a try...catch...finally statement to process the exception. The ShowThreadExceptionDialog method creates the message to display, and displays it in a message box.

' Creates a class to throw the error.
Public Class ErrorHandler
    Inherits System.Windows.Forms.Form    
    
    ' Inserts code to create a form with a button.
    
    ' Programs the button to throw the exception when clicked.
    Private Sub button1_Click(sender As Object, e As System.EventArgs)
        Throw New ArgumentException("The parameter was invalid")
    End Sub
    
    Public Shared Sub Main()
        ' Creates an instance of the methods that will handle the exception.
        Dim eh As New CustomExceptionHandler()
        
        ' Adds the event handler to to the event.
        AddHandler Application.ThreadException, AddressOf eh.OnThreadException
        
        ' Runs the application.
        Application.Run(New ErrorHandler())
    End Sub
    
End Class

' Creates a class to handle the exception event.
Friend Class CustomExceptionHandler
    
    'Handles 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
        
        ' Exits the program when the user clicks Abort.
        If result = DialogResult.Abort Then
            Application.Exit()
        End If
    End Sub
     
    ' Creates the error message and display it.
    Private Function ShowThreadExceptionDialog(e As Exception) As DialogResult
        Dim errorMsg As String = "An error occurred please contact the " & _
            "adminstrator with the following information:" & _
            Microsoft.VisualBasic.ControlChars.Cr & _
            Microsoft.VisualBasic.ControlChars.Cr
        errorMsg &= e.Message & Microsoft.VisualBasic.ControlChars.Cr & _
            Microsoft.VisualBasic.ControlChars.Cr & "Stack Trace:" & _
            Microsoft.VisualBasic.ControlChars.Cr & e.StackTrace
        Return MessageBox.Show(errorMsg, "Application Error", _
            MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop)
    End Function
End Class


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

Show:
© 2017 Microsoft