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.