Application.OnThreadException Method

Raises the ThreadException event.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public static void OnThreadException (
	Exception t
)
public static void OnThreadException (
	Exception t
)
public static function OnThreadException (
	t : Exception
)

Parameters

t

An Exception that represents the exception that was thrown.

This event enables an application to handle an exception intelligently. A window procedure calls this event when it receives a thread exception. Attach your event handlers to this event.

Raising an event invokes the event handler through a delegate. For more information, see Raising an Event.

The OnThreadException method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

NoteNote

The Application class has a ThreadException event. You can attach an event handler to this event to do whatever custom processing is necessary.

Notes to Inheritors When overriding OnThreadException in a derived class, be sure to call the base class's OnThreadException so that registered delegates receive the event.

The following code example raises a ThreadException event when button1 on the form is clicked. The example creates two classes. The ErrorHandler class creates the form and 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.

using System;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;

public class Form1 : Form
{
    // Inserts code to create a form with a button.
    Button button1;

    private Form1()
    {
        this.Size = new Size(600, 100);

        button1 = new Button();
        button1.Click += new EventHandler(button1_Click);
        button1.Text = "Click Me"; 
        button1.Location = new Point (10, 10);
        this.Controls.Add(button1);
    }
 
    // 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 the event.
       Application.ThreadException += new ThreadExceptionEventHandler(eh.OnThreadException);
 
       // Runs the application.
       Application.Run(new Form1());
    }
 }
 
 // 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 static class ErrorHandler extends 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");
    } //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 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.
private static class CustomExceptionHandler
{
    // Handles the exception event.
    private 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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show: