<PRE lang=cs>public static void Application_ThreadException(

    object sender, ThreadExceptionEventArgs e)


  // Handle exception.

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


<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(



     // Load the form

     Application.Run(new Form1());


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.");