ThreadExceptionEventArgs ThreadExceptionEventArgs ThreadExceptionEventArgs ThreadExceptionEventArgs Class

定義

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
継承
ThreadExceptionEventArgsThreadExceptionEventArgsThreadExceptionEventArgsThreadExceptionEventArgs
派生

させることにより、次の例をThreadExceptionイベントをクリックしてbutton1フォーム。The following example allows you to raise a ThreadException event by clicking button1 on a form. この例では、2 つのクラスを作成します。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.

Mainで、ErrorHandlerクラス、例外処理は、クラスのインスタンスの新しいインスタンスを作成するコード、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.

OnThreadExceptionメソッドで、CustomExceptionHandlerクラス、例では、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.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

注釈

AThreadExceptionEventArgsハンドルされない例外が発生したときに、スレッドが作成されます。A ThreadExceptionEventArgs is created by a thread when an unhandled exception occurs. ThreadExceptionEventArgs 含まれています、Exceptionが発生しました。ThreadExceptionEventArgs contains the Exception that occurred.

コンストラクター

ThreadExceptionEventArgs(Exception) ThreadExceptionEventArgs(Exception) ThreadExceptionEventArgs(Exception) ThreadExceptionEventArgs(Exception)

ThreadExceptionEventArgs クラスの新しいインスタンスを初期化します。Initializes a new instance of the ThreadExceptionEventArgs class.

プロパティ

Exception Exception Exception Exception

発生した Exception を取得します。Gets the Exception that occurred.

メソッド

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

指定したオブジェクトが、現在のオブジェクトと等しいかどうかを判断します。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

既定のハッシュ関数として機能します。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

現在のインスタンスの Type を取得します。Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

現在の Object の簡易コピーを作成します。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

現在のオブジェクトを表す文字列を返します。Returns a string that represents the current object.

(Inherited from Object)

適用対象

こちらもご覧ください