ThreadExceptionEventArgs Class
Provides data for the ThreadException event.
For a list of all members of this type, see ThreadExceptionEventArgs Members.
System.Object
System.EventArgs
System.Threading.ThreadExceptionEventArgs
[Visual Basic] Public Class ThreadExceptionEventArgs Inherits EventArgs [C#] public class ThreadExceptionEventArgs : EventArgs [C++] public __gc class ThreadExceptionEventArgs : public EventArgs [JScript] public class ThreadExceptionEventArgs extends EventArgs
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
A ThreadExceptionEventArgs is created by a thread when an unhandled exception occurs. ThreadExceptionEventArgs contains the Exception that occurred.
Example
[Visual Basic, C#, C++] 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.
[Visual Basic, C#, C++] 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.
[Visual Basic, C#, C++] 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.
[Visual Basic] ' 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 [C#] // 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); } } [C++] // Creates a class to handle the exception event. private __gc class CustomExceptionHandler { //Handles the exception event public: void OnThreadException(Object* /*sender*/, ThreadExceptionEventArgs* t) { DialogResult result = DialogResult::Cancel; try { result = this->ShowThreadExceptionDialog(t->Exception); } catch( Exception* ) { try { MessageBox::Show(S"Fatal Error", S"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 = S"An error occurred please contact the adminstrator with the following information:\n\n"; errorMsg = String::Concat( errorMsg, e->Message, S"\n\nStack Trace:\n", e->StackTrace ); return MessageBox::Show(errorMsg, S"Application Error", MessageBoxButtons::AbortRetryIgnore, MessageBoxIcon::Stop); } }; // Creates a class to throw the error. public __gc class ErrorHandler : public 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(S"The parameter was invalid"); } }; int main() { // 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, &CustomExceptionHandler::OnThreadException); // Runs the application. Application::Run(new ErrorHandler()); }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Threading
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
Assembly: System (in System.dll)
See Also
ThreadExceptionEventArgs Members | System.Threading Namespace | Thread | ThreadStart | ThreadExceptionEventHandler