ThreadExceptionEventArgs 类   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为 ThreadException 事件提供数据。
public ref class ThreadExceptionEventArgs : EventArgspublic class ThreadExceptionEventArgs : EventArgstype ThreadExceptionEventArgs = class
    inherit EventArgsPublic Class ThreadExceptionEventArgs
Inherits EventArgs- 继承
- 派生
示例
以下示例允许通过单击button1窗体引发ThreadException事件。 该示例创建两个类。 类 ErrorHandler 创建引发 事件的窗体和按钮。 类 CustomExceptionHandler 提供用于处理异常的方法。
在 Main 类中 ErrorHandler ,代码创建异常处理类的新实例,即 的 CustomExceptionHandler实例。 然后,实例将添加到 事件,并运行应用程序。
              OnThreadException在 类的 方法中CustomExceptionHandler,该示例使用 语句try...catch...finally来处理异常。 方法 ShowThreadExceptionDialog 创建要显示的消息,并将其显示在消息框中。
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.SuspendLayout();
       this.button1.Location = new System.Drawing.Point(100, 43);
       this.button1.Size = new System.Drawing.Size(75, 23);
       this.button1.Text = "Click!";
       this.Controls.Add(this.button1);
       this.button1.Click += new EventHandler(this.button1_Click);
       this.Text = "ThreadException";
       this.ResumeLayout(false);
    }
    // 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)
          Application.Exit();
    }
    // 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.SuspendLayout()
      Me.button1.Location = New System.Drawing.Point(100, 43)
      Me.button1.Size = New System.Drawing.Size(75, 23)
      Me.button1.Text = "Click!"
      Me.Controls.Add(Me.button1)
      Me.Text = "ThreadException"
      Me.ResumeLayout(False)
   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
            Application.Exit()
        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
注解
发生 ThreadExceptionEventArgs 未经处理的异常时,线程会创建 。 ThreadExceptionEventArgs 包含 Exception 发生的 。
构造函数
| ThreadExceptionEventArgs(Exception) | 初始化 ThreadExceptionEventArgs 类的新实例。 | 
属性
| Exception | 获取已发生的 Exception。 | 
方法
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) |