Application::ThreadException Event
Occurs when an untrapped thread exception is thrown.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
This event allows your Windows Forms application to handle otherwise unhandled exceptions that occur in Windows Forms threads. Attach your event handlers to the ThreadException event to deal with these exceptions, which will leave your application in an unknown state. Where possible, exceptions should be handled by a structured exception handling block.
You can change whether this callback is used for unhandled Windows Forms thread exceptions by setting SetUnhandledExceptionMode. To catch exceptions that occur in threads not created and owned by Windows Forms, use the UnhandledException event handler.
Note |
|---|
To guarantee that no activations of this event are missed, you must attach a handler before you call Application::Run. |
Caution |
|---|
Because this is a static event, you must detach your event handlers when your application is disposed, or memory leaks will result. |
The following code example sets event handlers for exceptions that occur on Windows Forms threads and exceptions that occur on other threads. It sets SetUnhandledExceptionMode so that all exceptions are handled by the application, regardless of the settings in the application's user configuration file. It uses the ThreadException event to handle UI thread exceptions, and the UnhandledException event to handle non-UI thread exceptions. Since UnhandledException cannot prevent an application from terminating, the example simply logs the error in the application event log before termination.
This example assumes that you have defined two Button controls, button1 and button2, on your Form class.
// 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 ); } };
- SecurityPermission
for the immediate caller to call unmanaged code when adding a handler to this event. Associated enumeration: SecurityPermissionFlag::UnmanagedCode
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note