Application.ThreadException イベントとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Application.ThreadException イベントの意味・解説 

Application.ThreadException イベント

トラップされないスレッド例外スローされると、発生します

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

Public Shared Event ThreadException
 As ThreadExceptionEventHandler
Dim handler As ThreadExceptionEventHandler

AddHandler Application.ThreadException, handler
public static event ThreadExceptionEventHandler
 ThreadException
public:
static event ThreadExceptionEventHandler^ ThreadException {
    void add (ThreadExceptionEventHandler^ value);
    void remove (ThreadExceptionEventHandler^ value);
}
/** @event */
public static void add_ThreadException
 (ThreadExceptionEventHandler value)

/** @event */
public static void remove_ThreadException
 (ThreadExceptionEventHandler value)
JScript では、イベント使用できますが、新規に宣言することはできません。
解説解説
使用例使用例

フォーム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 the code to create a form with a button.

   ' Programs the button to throw an exception when clicked.
   Private Sub button1_Click(sender As
 object, e As System.EventArgs)
      Throw New ArgumentException("The
 parameter was invalid")
   End Sub

   <STAThread()> _
   Shared Sub Main()
      ' Creates an instance of the methods that will handle the exception.
      Dim eh As CustomExceptionHandler = New
 CustomExceptionHandler()

      ' Adds the event handler 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.
Private Class CustomExceptionHandler

   ' Handles the exception event.
   Public Sub OnThreadException(sender As
 object, t As ThreadExceptionEventArgs) 
      Dim result As DialogResult = System.Windows.Forms.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 = System.Windows.Forms.DialogResult.Abort) Then
         Application.Exit()
      End If
   End Sub

   ' Creates the error message and displays it.
   Private Function ShowThreadExceptionDialog(e
 As Exception) As DialogResult
      Dim errorMsg As StringWriter = New
 StringWriter()
      errorMsg.WriteLine("An error occurred please contact the
 adminstrator with the following information:")
      errorMsg.WriteLine("")
      errorMsg.WriteLine(e.Message)
      errorMsg.WriteLine("")
      errorMsg.WriteLine("Stack Trace:")
      errorMsg.WriteLine(e.StackTrace)
      Return MessageBox.Show(errorMsg.ToString(), "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 the code to create a form with a button.
 
    // Programs the button to throw an 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 displays 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 throw the error.
public:
   ref class ErrorHandler: public System::Windows::Forms::Form
   {
      // Inserts the code to create a form with a button.

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

   public:
      static void 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, &Form1::CustomExceptionHandler::OnThreadException
 );
         
         // Runs the application.
         Application::Run( gcnew ErrorHandler );
      }
   };

   // Creates a class to handle the exception event.
internal:
   ref class CustomExceptionHandler
   {
      // Handles the exception event.
   public:
      void OnThreadException( Object^ /*sender*/, ThreadExceptionEventArgs^
 t )
      {
         System::Windows::Forms::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();
         }
      }

      // Creates the error message and displays it.
   private:
      System::Windows::Forms::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 static class ErrorHandler
 extends System.Windows.Forms.Form
{
    // Inserts the code to create a form with a button.
    // Programs the button to throw an exception when clicked.
    private void button1_Click(Object sender,
 System.EventArgs e)
    {
        throw new ArgumentException("The parameter was invalid");
    } //button1_Click

    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.add_ThreadException(
            new ThreadExceptionEventHandler(eh.OnThreadException));

        // Runs the application.
        Application.Run(new ErrorHandler());
    } //main
} //ErrorHandler

// Creates a class to handle the exception event.
static class CustomExceptionHandler
{
    // Handles the exception event.
    public void OnThreadException(Object sender,
 ThreadExceptionEventArgs t)
    {
        DialogResult result = DialogResult.Cancel;
        try {
            result = this.ShowThreadExceptionDialog(t.get_Exception());
        }
        catch (System.Exception exp) {
            try {
                MessageBox.Show("Fatal Error", "Fatal Error",
 
                    MessageBoxButtons.AbortRetryIgnore, 
                    MessageBoxIcon.Stop);
            }
            finally {
                Application.Exit();
            }
        }

        // Exits the program when the user clicks Abort.
        if (result.Equals(DialogResult.Abort)) {
            Application.Exit();
        }
    } //OnThreadException

    // Creates the error message and displays it.
    private DialogResult ShowThreadExceptionDialog(System.Exception
 e)
    {
        String errorMsg = "An error occurred please contact the"
            + " administrator with the following information:\n\n";
        errorMsg = errorMsg + e.get_Message() + "\n\nStack Trace:\n"
            + e.get_StackTrace();
        return MessageBox.Show(errorMsg, "Application Error"
,
            MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
    } //ShowThreadExceptionDialog
} //CustomExceptionHandler

public static void main(String[]
 args)
{
    ErrorHandler.main(args);
}//main
.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からApplication.ThreadException イベントを検索した結果を表示しています。
Weblioに収録されているすべての辞書からApplication.ThreadException イベントを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からApplication.ThreadException イベント を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Application.ThreadException イベント」の関連用語

Application.ThreadException イベントのお隣キーワード
検索ランキング

   

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



Application.ThreadException イベントのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS