閱讀英文版本

分享方式:


ThreadExceptionEventArgs 類別

定義

提供 ThreadException 事件的資料。

C#
public class ThreadExceptionEventArgs : EventArgs
繼承
ThreadExceptionEventArgs
衍生

範例

下列範例可讓您按一下 button1 表單來引發 ThreadException 事件。 此範例會建立兩個類別。 類別 ErrorHandler 會建立會引發事件的表單和按鈕。 類別 CustomExceptionHandler 提供處理例外狀況的方法。

Main 類別 ErrorHandler 中,程式碼會建立例外狀況處理類別的新實例,也就是 的 CustomExceptionHandler 實例。 然後,實例會新增至 事件,並執行應用程式。

OnThreadException在 類別的 CustomExceptionHandler 方法中,此範例會使用 try...catch...finally 語句來處理例外狀況。 ShowThreadExceptionDialog方法會建立要顯示的訊息,並將其顯示在訊息方塊中。

C#
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);
    }
 }

備註

ThreadExceptionEventArgs發生未處理的例外狀況時,執行緒會建立 。 ThreadExceptionEventArgs 包含 Exception 發生的 。

建構函式

ThreadExceptionEventArgs(Exception)

初始化 ThreadExceptionEventArgs 類別的新執行個體。

屬性

Exception

取得發生的 Exception

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另請參閱