Roland
Roland

Reputation: 5226

Why does my app not stop after an unhandled exception?

I want to find out how the Windows Event Log works when my app crashes, so I added a test button to throw new Exception(). To my surprise, the app just keeps running. Windows shows a dialog with the options to Continue or Quit, and clicking Continue, the app keeps running. I expected that the app would crash after continuing after an unhandled exception.

Most blogs on this topic test with a small console app that does a divide by zero, but I thought it does not add much trouble to create a small Forms app instead. In the real world I need to know how my Forms apps behave. Here is the code with two buttons: one to update a time display, to prove that the app is really running, and one to throw an unhandled exception.

using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            label_Msg.Text = DateTime.Now.ToString();
        }

        private void button_CrashNow_Click(object sender, EventArgs e)
        {
            throw new DivideByZeroException("This is Sandbox Crash Test");
            label_Msg.Text = DateTime.Now.ToString();
        }

        private void button_UpdateTime_Click(object sender, EventArgs e)
        {
            label_Msg.Text = DateTime.Now.ToString();
        }
    }
}

Running in the VS2017 debugger with F5 it will stop at the exception. Continuing with F5 would end the app.

But running it from the bin/debug folder, by double-clicking the .exe file, the app will NOT stop or quit, no matter how often I click the CrashNow button. By clicking the Update button, the time display updates as if no exception had happened.

The only thing the unhandled exception does is that the time update for that button does not work.

How is this possible?


By the way, my question is not about the difference between Application.ThreadException and AppDomain.CurrentDomain.UnhandledException. In fact, I have never heard of those exceptions. My question is also not about having to handle both of those exceptions. My question and that other question seem to mention the same Application.SetUnhandledExceptionMode() method. This surely is an interesting method. In my opinion, my question is not duplicate. However, a link to that other question may be useful for more in-depth understanding of what is going on under the WinForm hood.

Upvotes: 1

Views: 828

Answers (1)

Roland
Roland

Reputation: 5226

After reading the ms doc on the link suggested by the comment of steve16351 I learned that the Windows Forms app have a default exception handler.

By disabling that handler before creating the Form in Program.cs using:

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);

the unhandled exception will cause a different windows dialog, with options to

  • check online for a solution and close the program
  • close the program
  • debug the program

so now there is no way to continue with the Update button.

PS: I am sorry to see that he deleted his comment just before I wanted to upvote it


Update:

How silly this question may sound, it turned out that this setting is essential if you are thinking of catching all exceptions of your WinForms app with a try-catch block in Program.cs. Without this setting, exceptions in a Form are caught in such block only when running in the VS debugger, but not from a double-click on the *.exe in an Explorer window.

An exception thrown in a Form apparently is considered unhandled if it is not caught in a Form. You may try to catch it in the Program.cs that created the Form, but the default application handler may catch it first. Strange but true :-)

Upvotes: 3

Related Questions