ThreadExceptionEventArgs Constructor

Initializes a new instance of the ThreadExceptionEventArgs class.

Namespace:  System.Threading
Assembly:  System (in System.dll)

public ThreadExceptionEventArgs(
	Exception t
)

Parameters

t
Type: System.Exception

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 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);
    }
 }

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft