I've come across such a situation. WinForms application has two forms. The main form has a button, when user clicks it, modal dialog is shown. The dialog form has a button too, when user clicks it, exception is thrown.
Exception handling differs, when application running under debugger and running itself. Here's minimal code, reproducing this behavior:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
using (var dialog = new Form2())
{
dialog.ShowDialog();
}
}
catch (Exception ex)
{
MessageBox.Show("Oops! " + ex.Message);
}
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
}
When debugging, raising an exception closes the dialog, and exception handler in Form1.button1_Click handles exception.
When running application itself, raising an exception doesn't close the dialog. Instead of this, default Application.ThreadException handler being invoked.
Why (and what for) does the behavior differs? How to bring it into accord with each other?