Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
{
    // simply check if file exists
    if (!File.Exists(comboBox1.Text))
    {
         // if not, this exception should be caught by the
         // unhandled exception handlers found in Program.cs
         throw new ApplicationException("File does not exist.");
    } else {
        MessageBox.Show("File found.");
    }
}

private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '\r')
    {
        // user has directly typed a filename in the combobox
        // and pressed "Enter"
        // execute same code as if chosen from dropdown list
        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!
Posted

Have you stepped through the code to make sure that it's being called as you expect ? Don't use \r, use Keys.Return, which means using keyup or keydown as keypress is not passed an instance of the Keys enum. It's just cleaner that way.
 
Share this answer
 
That is funny. Not only is it happening in my larger application, but also in the simple example I provided.

I have also painstakingly stepped through the code tens of times in the debugger. It works as I expect up to the point of throwing the exception. The debugger then stops on the next line with the message "ApplicationException was unhandled by user code.". But... in the case of a dropdown choice, then proceeds to the first line of my exception handler... in the case of a keypress, disappears and I am returned back to the main form to begin again.

Thank you for your time in looking into this! And thanks for the Keys.Return tip. Not sure what I'll do now...
 
Share this answer
 
That's funny, as I can't seem to replicate this. It works every time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900