I have a situation where I am specifically creating and throwing an exception, but it seems to be getting lost under certain conditions.
I have been able to recreate the situation in a simple C# Windows Application project.
Consider a simple form with just one ComboBox control and a few filenames (which exist and don’t exist) in its Items Collection.
If the user enters a filename that does not exist, an exception should be created and thrown.
Here’s the thing:
If the invalid filename is chosen from the dropdown, everything works as expected.
If the invalid filename is typed directly in the combobox and ‘Enter’ is pressed, the same exception is lost. Why is this happening?
I am using the same code for both situations:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (!File.Exists(comboBox1.Text))
{
throw new ApplicationException("File does not exist.");
} else {
MessageBox.Show("File found.");
}
}
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
comboBox1_SelectedIndexChanged(sender, e);
}
}
(I have defined the usual catchall exception handlers in Program.cs)
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += new
ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Any help will be greatly appreciated!