ThreadExceptionEventArgsとは? わかりやすく解説

ThreadExceptionEventArgs クラス

ThreadException イベントのデータを提供します。

名前空間: System.Threading
アセンブリ: System (system.dll 内)
構文構文

Public Class ThreadExceptionEventArgs
    Inherits EventArgs
Dim instance As ThreadExceptionEventArgs
public class ThreadExceptionEventArgs : EventArgs
public ref class ThreadExceptionEventArgs :
 public EventArgs
public class ThreadExceptionEventArgs extends
 EventArgs
public class ThreadExceptionEventArgs extends
 EventArgs
解説解説

ThreadExceptionEventArgs は、処理されない例外が発生したときにスレッドによって作成されます。ThreadExceptionEventArgs は、発生した Exception を保持します。

使用例使用例

フォーム上の button1 をクリックすることによって、ThreadException イベントを発生させる例を次に示します。この例では、次の 2 つのクラスを作成します。ErrorHandler クラスは、フォームと、イベントを発生させるボタンを作成します。CustomExceptionHandler クラスは、例外を処理するためのメソッドを提供します。

ErrorHandler クラスの Main では、コードが例外処理クラスの新しいインスタンス (CustomExceptionHandler のインスタンス) を作成します。次に、作成されたインスタンスがイベントに追加され、アプリケーションが実行 (Run) されます。

この例では、CustomExceptionHandler クラスの OnThreadException メソッドで、try...catch...finally ステートメントを使用して例外を処理しています。ShowThreadExceptionDialog メソッドは、表示するメッセージを作成し、そのメッセージをメッセージ ボックスに表示します。

' 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

// Creates a class to throw the error.
 public class ErrorHandler : System.Windows.Forms.Form
 {
 
    // Inserts code to create a form with a button.
 
    // Programs the button to throw the exception when clicked.
    private void button1_Click(object sender,
 System.EventArgs e) {
       throw new ArgumentException("The parameter was invalid");
    }
 
    public static void Main(string[]
 args) {
       // Creates an instance of the methods that will handle the exception.
       CustomExceptionHandler eh = new CustomExceptionHandler();
 
       // Adds the event handler to to the event.
       Application.ThreadException += new ThreadExceptionEventHandler(eh.OnThreadException);
 
       // Runs the application.
       Application.Run(new ErrorHandler());
    }
 }
 
 // Creates a class to handle the exception event.
 internal class CustomExceptionHandler {
 
    //Handles the exception event
    public void OnThreadException(object sender,
 ThreadExceptionEventArgs t) 
    {
       DialogResult result = DialogResult.Cancel;
       try {
          result = this.ShowThreadExceptionDialog(t.Exception);
       }
       catch {
          try {
             MessageBox.Show("Fatal Error", "Fatal Error", 
                MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
          }
          finally {
             Application.Exit();
          }
       }
 
       // Exits the program when the user clicks Abort.
       if (result == DialogResult.Abort) 
          Application.Exit();
    }
 
    // Creates the error message and display it.
    private DialogResult ShowThreadExceptionDialog(Exception e)
 {
       string errorMsg = "An error occurred please contact
 the adminstrator " +
            "with the following information:\n\n";
       errorMsg = errorMsg + e.Message + "\n\nStack Trace:\n" + e.StackTrace;
       return MessageBox.Show(errorMsg, "Application Error",
 
            MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
    }
 }
 
// Creates a class to handle the exception event.
private ref class CustomExceptionHandler
{
public:

   //Handles the exception event
   void OnThreadException( Object^ /*sender*/, ThreadExceptionEventArgs^
 t )
   {
      DialogResult result = DialogResult::Cancel;
      try
      {
         result = this->ShowThreadExceptionDialog( t->Exception
 );
      }
      catch ( Exception^ ) 
      {
         try
         {
            MessageBox::Show( "Fatal Error", "Fatal Error", MessageBoxButtons::AbortRetryIgnore,
 MessageBoxIcon::Stop );
         }
         finally
         {
            Application::Exit();
         }

      }

      
      // Exits the program when the user clicks Abort.
      if ( result == DialogResult::Abort )
            Application::Exit();
   }


private:

   // Creates the error message and display it.
   DialogResult ShowThreadExceptionDialog( Exception^ e )
   {
      String^ errorMsg = "An error occurred please contact the adminstrator
 with the following information:\n\n";
      errorMsg = String::Concat( errorMsg, e->Message, "\n\nStack Trace:\n",
 e->StackTrace );
      return MessageBox::Show( errorMsg, "Application Error",
 MessageBoxButtons::AbortRetryIgnore, MessageBoxIcon::Stop );
   }

};


// Creates a class to throw the error.
public ref class ErrorHandler: public
 System::Windows::Forms::Form
{
private:

   // Inserts code to create a form with a button.
   // Programs the button to throw the exception when clicked.
   void button1_Click( Object^ /*sender*/, System::EventArgs^
 /*e*/ )
   {
      throw gcnew ArgumentException( "The parameter was invalid" );
   }

};

int main()
{
   
   // Creates an instance of the methods that will handle the exception.
   CustomExceptionHandler^ eh = gcnew CustomExceptionHandler;
   
   // Adds the event handler to to the event.
   Application::ThreadException += gcnew ThreadExceptionEventHandler( eh, &CustomExceptionHandler::OnThreadException
 );
   
   // Runs the application.
   Application::Run( gcnew ErrorHandler );
}

継承階層継承階層
System.Object
   System.EventArgs
    System.Threading.ThreadExceptionEventArgs
       Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ThreadExceptionEventArgs メンバ
System.Threading 名前空間
Thread クラス
ThreadStart
ThreadExceptionEventHandler

ThreadExceptionEventArgs コンストラクタ

ThreadExceptionEventArgs クラスの新しいインスタンスを初期化します。

名前空間: System.Threading
アセンブリ: System (system.dll 内)
構文構文

Dim t As Exception

Dim instance As New ThreadExceptionEventArgs(t)
public ThreadExceptionEventArgs (
    Exception t
)
public:
ThreadExceptionEventArgs (
    Exception^ t
)
public ThreadExceptionEventArgs (
    Exception t
)
public function ThreadExceptionEventArgs (
    t : Exception
)

パラメータ

t

発生した Exception。

使用例使用例

フォーム上の button1 をクリックすることによって、ThreadException イベントを発生させる例を次に示します。この例では、次の 2 つのクラスを作成します。ErrorHandler クラスは、フォームと、イベントを発生させるボタンを作成します。CustomExceptionHandler クラスは、例外を処理するためのメソッドを提供します。

ErrorHandler クラスの Main では、コードが例外処理クラスの新しいインスタンス (CustomExceptionHandler のインスタンス) を作成します。次に、作成されたインスタンスがイベントに追加され、アプリケーションが実行 (Run) されます。

この例では、CustomExceptionHandler クラスの OnThreadException メソッドで、try...catch...finally ステートメントを使用して例外を処理しています。ShowThreadExceptionDialog メソッドは、表示するメッセージを作成し、そのメッセージをメッセージ ボックスに表示します。

' 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

// Creates a class to throw the error.
 public class ErrorHandler : System.Windows.Forms.Form
 {
 
    // Inserts code to create a form with a button.
 
    // Programs the button to throw the exception when clicked.
    private void button1_Click(object sender,
 System.EventArgs e) {
       throw new ArgumentException("The parameter was invalid");
    }
 
    public static void Main(string[]
 args) {
       // Creates an instance of the methods that will handle the exception.
       CustomExceptionHandler eh = new CustomExceptionHandler();
 
       // Adds the event handler to to the event.
       Application.ThreadException += new ThreadExceptionEventHandler(eh.OnThreadException);
 
       // Runs the application.
       Application.Run(new ErrorHandler());
    }
 }
 
 // Creates a class to handle the exception event.
 internal class CustomExceptionHandler {
 
    //Handles the exception event
    public void OnThreadException(object sender,
 ThreadExceptionEventArgs t) 
    {
       DialogResult result = DialogResult.Cancel;
       try {
          result = this.ShowThreadExceptionDialog(t.Exception);
       }
       catch {
          try {
             MessageBox.Show("Fatal Error", "Fatal Error", 
                MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
          }
          finally {
             Application.Exit();
          }
       }
 
       // Exits the program when the user clicks Abort.
       if (result == DialogResult.Abort) 
          Application.Exit();
    }
 
    // Creates the error message and display it.
    private DialogResult ShowThreadExceptionDialog(Exception e)
 {
       string errorMsg = "An error occurred please contact
 the adminstrator " +
            "with the following information:\n\n";
       errorMsg = errorMsg + e.Message + "\n\nStack Trace:\n" + e.StackTrace;
       return MessageBox.Show(errorMsg, "Application Error",
 
            MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
    }
 }
 
// Creates a class to handle the exception event.
private ref class CustomExceptionHandler
{
public:

   //Handles the exception event
   void OnThreadException( Object^ /*sender*/, ThreadExceptionEventArgs^
 t )
   {
      DialogResult result = DialogResult::Cancel;
      try
      {
         result = this->ShowThreadExceptionDialog( t->Exception
 );
      }
      catch ( Exception^ ) 
      {
         try
         {
            MessageBox::Show( "Fatal Error", "Fatal Error", MessageBoxButtons::AbortRetryIgnore,
 MessageBoxIcon::Stop );
         }
         finally
         {
            Application::Exit();
         }

      }

      
      // Exits the program when the user clicks Abort.
      if ( result == DialogResult::Abort )
            Application::Exit();
   }


private:

   // Creates the error message and display it.
   DialogResult ShowThreadExceptionDialog( Exception^ e )
   {
      String^ errorMsg = "An error occurred please contact the adminstrator
 with the following information:\n\n";
      errorMsg = String::Concat( errorMsg, e->Message, "\n\nStack Trace:\n",
 e->StackTrace );
      return MessageBox::Show( errorMsg, "Application Error",
 MessageBoxButtons::AbortRetryIgnore, MessageBoxIcon::Stop );
   }

};


// Creates a class to throw the error.
public ref class ErrorHandler: public
 System::Windows::Forms::Form
{
private:

   // Inserts code to create a form with a button.
   // Programs the button to throw the exception when clicked.
   void button1_Click( Object^ /*sender*/, System::EventArgs^
 /*e*/ )
   {
      throw gcnew ArgumentException( "The parameter was invalid" );
   }

};

int main()
{
   
   // Creates an instance of the methods that will handle the exception.
   CustomExceptionHandler^ eh = gcnew CustomExceptionHandler;
   
   // Adds the event handler to to the event.
   Application::ThreadException += gcnew ThreadExceptionEventHandler( eh, &CustomExceptionHandler::OnThreadException
 );
   
   // Runs the application.
   Application::Run( gcnew ErrorHandler );
}

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ThreadExceptionEventArgs クラス
ThreadExceptionEventArgs メンバ
System.Threading 名前空間
Thread クラス
ThreadStart

ThreadExceptionEventArgs プロパティ


パブリック プロパティパブリック プロパティ

参照参照

関連項目

ThreadExceptionEventArgs クラス
System.Threading 名前空間
Thread クラス
ThreadStart
ThreadExceptionEventHandler

ThreadExceptionEventArgs メソッド


ThreadExceptionEventArgs メンバ

ThreadException イベントのデータを提供します。

ThreadExceptionEventArgs データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド ThreadExceptionEventArgs ThreadExceptionEventArgs クラスの新しいインスタンスを初期化します。
パブリック プロパティパブリック プロパティ
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

ThreadExceptionEventArgs クラス
System.Threading 名前空間
Thread クラス
ThreadStart
ThreadExceptionEventHandler



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「ThreadExceptionEventArgs」の関連用語

ThreadExceptionEventArgsのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ThreadExceptionEventArgsのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS