1

In winforms, if an unhandled exception is encountered, a dialogue pops up alerting you that an exception occurred. This has buttons to continue execution or quit the application. What I was wondering is what state the app continues from if you select the continue button. My instinct would have been that it operated like in a debugger, continuing on the next statement after the exception, but this doesn't seem to be the case. To demonstrate this, I made a winforms app with a TextBox textBox1 and a button button1 with the following functions:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }
    private void button1_Click(object Sender, EventArgs e) {
        textBox1.Text = "Line before exception";
        throw_Exception();
        textBox1.Text = "Line after exception";
    }
    private void throw_Exception() => throw new Exception();
}

With this you see "Line before exception", then the exception dialogue (yay), but even if you hit continue, you do not reach "Line after exception". Obviously this is a silly example, but I am still unclear on what the continue button is actually doing. If the exception was thrown at the bottom of a large hierarchy does execution of everything up to the UI level stop? Does garbage collection run on any objects created before the exception? What if the exception is thrown on one thread of a multithreaded program?

Disclaimer: Yes, I know that there shouldn't be unhandled exceptions, and I certainly should not be throwing a general System.Exception. I am just asking how the continue operation works under the hood and how to determine what state a system is in if the continue button is pressed.

bisen2
  • 486
  • 1
  • 4
  • 10
  • I just tried this `int a = 1; int b = 0; int c = a / b; int x = 0;` on exception continue id didn't run the `int x =0;` line – Vivek Nuna Jun 09 '20 at 13:31
  • 2
    This dialog is only shown for code that runs on the UI thread, and choosing "Continue" causes your application to give control back to the OS. So the remainder of your code does not run as if it's done handling `button1_Click` - "Continue" just means your application doesn't quit and it will respond again to the next event. Also see this answer: https://stackoverflow.com/a/2629411/292411 – C.Evenhuis Jun 09 '20 at 13:42
  • @C.Evenhuis thanks, that helps a lot. – bisen2 Jun 09 '20 at 13:59

0 Answers0