ThreadExceptionEventArgs 类


ThreadException 事件提供数据。Provides data for the ThreadException event.

public ref class ThreadExceptionEventArgs : EventArgs
public class ThreadExceptionEventArgs : EventArgs
type ThreadExceptionEventArgs = class
    inherit EventArgs
Public Class ThreadExceptionEventArgs
Inherits EventArgs


下面的示例允许通过单击窗体上的 button1 来引发 ThreadException 事件。The following example allows you to raise a ThreadException event by clicking button1 on a form. 该示例创建了两个类。The example creates two classes. ErrorHandler 类创建窗体和引发事件的按钮。The ErrorHandler class creates the form and the button that raises the event. CustomExceptionHandler 类提供处理异常的方法。The CustomExceptionHandler class provides the methods to handle the exception.

ErrorHandler 类的 Main 中,代码将创建异常处理类的新实例,即,CustomExceptionHandler的实例。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.

CustomExceptionHandler 类的 OnThreadException 方法中,该示例使用了 try...catch...finally 语句来处理异常。In the OnThreadException method in the CustomExceptionHandler class, the example uses a try...catch...finally statement to process the exception. ShowThreadExceptionDialog 方法创建要显示的消息,并将其显示在消息框中。The ShowThreadExceptionDialog method creates the message to display, and displays it in a message box.

using System;
using System.Threading;
using System.Windows.Forms;

// Create a form with a button that, when clicked, throws an exception.
 public class ErrorForm : System.Windows.Forms.Form
    internal Button button1;

    public ErrorForm() : base()
       // Add the button to the form.
       this.button1 = new System.Windows.Forms.Button();
       this.button1.Location = new System.Drawing.Point(100, 43);
       this.button1.Size = new System.Drawing.Size(75, 23);
       this.button1.Text = "Click!";
       this.button1.Click += new EventHandler(this.button1_Click);

       this.Text = "ThreadException";

    // Throw an exception when the button is clicked.
    private void button1_Click(object sender, System.EventArgs e)
       throw new ArgumentException("The parameter was invalid");
    public static void Main()
       // Add the event handler.
       Application.ThreadException += new ThreadExceptionEventHandler(CustomExceptionHandler.OnThreadException);

       // Start the example.
       Application.Run(new ErrorForm());
 // Create a class to handle the exception event.
 internal class CustomExceptionHandler
     // Handle the exception event
    public static void OnThreadException(object sender, ThreadExceptionEventArgs t)
       DialogResult result = ShowThreadExceptionDialog(t.Exception);

       // Exit the program when the user clicks Abort.
       if (result == DialogResult.Abort) 
    // Create and display the error message.
    private static DialogResult ShowThreadExceptionDialog(Exception e)
       string errorMsg = "An error occurred.  Please contact the adminstrator " +
            "with the following information:\n\n";
       errorMsg += String.Format("Exception Type: {0}\n\n", e.GetType().Name);
       errorMsg += "\n\nStack Trace:\n" + e.StackTrace;
       return MessageBox.Show(errorMsg, "Application Error", 
            MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
Imports System.Threading
Imports System.Windows.Forms

' Create a form with a button that, when clicked, throws an exception.
Public Class ErrorForm : Inherits Form
    Friend WithEvents button1 As Button

    Public Sub New()
       ' Add the button to the form.
      Me.button1 = New System.Windows.Forms.Button()
      Me.button1.Location = New System.Drawing.Point(100, 43)
      Me.button1.Size = New System.Drawing.Size(75, 23)
      Me.button1.Text = "Click!"

      Me.Text = "ThreadException"
   End Sub

    ' Throw an exception when the button is clicked.
    Private Sub button1_Click(sender As Object, e As System.EventArgs) _
                Handles button1.Click
        Throw New ArgumentException("The parameter was invalid.")
    End Sub
    Public Shared Sub Main()
        ' Add the event handler.
        AddHandler Application.ThreadException,
                   AddressOf CustomExceptionHandler.OnThreadException
        ' Start the example.
        Application.Run(New ErrorForm())
    End Sub
End Class

' Create a class to handle the exception event.
Friend Class CustomExceptionHandler
    'Handle the exception event.
    Public Shared Sub OnThreadException(sender As Object, t As ThreadExceptionEventArgs)
        Dim result As DialogResult = ShowThreadExceptionDialog(t.Exception)

        ' Exit the program when the user clicks Abort.
        If result = DialogResult.Abort Then
        End If
    End Sub
    ' Create and display the error message.
    Private Shared Function ShowThreadExceptionDialog(e As Exception) As DialogResult
        Dim errorMsg As String = "An error occurred.  Please contact the " &
            "adminstrator with the following information:" &
            vbCrLf & vbCrLf
        errorMsg &= "Exception Type: " & e.GetType().Name & vbCrLf & vbCrLf
        errorMsg &= e.Message & vbCrLf & vbCrLf
        errorMsg &= "Stack Trace: " & vbCrLf & e.StackTrace

        Return MessageBox.Show(errorMsg, "Application Error",
               MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop)
    End Function
End Class


当发生未处理的异常时,由线程创建 ThreadExceptionEventArgsA ThreadExceptionEventArgs is created by a thread when an unhandled exception occurs. ThreadExceptionEventArgs 包含发生的 ExceptionThreadExceptionEventArgs contains the Exception that occurred.



初始化 ThreadExceptionEventArgs 类的新实例。Initializes a new instance of the ThreadExceptionEventArgs class.



获取已发生的 ExceptionGets the Exception that occurred.



确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)