<PRE lang=cs>public static void Application_ThreadException(

    object sender, ThreadExceptionEventArgs e)

{

  // Handle exception.

  // The exception object is contained in e.Exception.

}</PRE>

<P>And this is how we hook it up.</P><PRE lang=cs>static void Main()

{

    // Subscribe to thread (unhandled) exception events

    // (Alternatively, could do this in Form_Load)

    Application.ThreadException += new ThreadExceptionEventHandler(

        Application_ThreadException);

 

     // Load the form

     Application.Run(new Form1());

}</PRE>

So the Add function can now look like this.

private void btnAdd_Click(object sender, System.EventArgs e)
{
    txtName.Text = "Kevin";
    throw new InvalidOperationException("Invalid operation.");
}