ThreadExceptionEventArgs Class
Provides data for the ThreadException event.
System::EventArgs
System.Threading::ThreadExceptionEventArgs
Microsoft.VisualBasic.ApplicationServices::UnhandledExceptionEventArgs
Assembly: System (in System.dll)
The ThreadExceptionEventArgs type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | ThreadExceptionEventArgs | Initializes a new instance of the ThreadExceptionEventArgs class. |
| Name | Description | |
|---|---|---|
![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
A ThreadExceptionEventArgs is created by a thread when an unhandled exception occurs. ThreadExceptionEventArgs contains the Exception that occurred.
The following example allows you to raise a ThreadException event by clicking button1 on a form. The example creates two classes. The ErrorHandler class creates the form and the button that raises the event. The CustomExceptionHandler class provides the methods to handle the exception.
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.
In the OnThreadException method in the CustomExceptionHandler class, the example uses a try...catch...finally statement to process the exception. The ShowThreadExceptionDialog method creates the message to display, and displays it in a message box.
// 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 ); }
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.
