简体   繁体   中英

try catch not working properly in WinForms Application

I am trying to catch an exception in a legacy winforms applications but somehow the exception is always captured by the global exception handling.

In the Program.cs I have the following code :

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

Application.ThreadException += Application_ThreadException;
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
    //throw new NotImplementedException();
    MessageBox.Show(e.Exception.Message);
}

and in the form I have the following code:

private void gttBindingSourceLosplaatsen_PositionChanged(object sender, EventArgs e)
{
    try
    {
        bool enabled = ((DataTable)gttBindingSourceLosplaatsen.DataSource).Rows.Count > 0;
        buttonEditLosplaats.Enabled = enabled;
        buttonDeleteLosplaats.Enabled = enabled;
    }
    catch 
    {
        MessageBox.Show("what is this ?");
    }
}

Now when the line "buttonDeleteLosplaats.Enable = enabled;" is executed an exception occurs. Now I expect to fall into the catch block and show the message "What is this ?" but that does not happen, I always fall into the global exception handler instead.

What could cause this ? There are a lot of other try..catch constructs in this application that do work as I expect and do fall into the catch block, but not this one. Does anybody has an idea why I do not fall into the local try...catch block but into the global exception handler ?

The odds that you've found the code that throws this exception are zero, the Enabled property cannot throw an "Index -1 does not have a value" exception.

 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

That is your real problem. Writing an event handler for Application.ThreadException is fine but that only works well when you don't need to debug your program. With UnhandledExceptionMode.CatchException activated, you get pretty blind when the program throws. The debugger can no longer stop and show you the problem, it is now your ThreadException event handler that swallows it. Technically you can still get it to stop, you need to use Debug + Exceptions, tick the Thrown checkbox for CLR exceptions.

But solve this the correct way, don't enable this event handler when you are debugging. Fix:

    if (!System.Diagnostics.Debugger.IsAttached) {
        Application.ThreadException += Application_ThreadException;
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
    }

You are catching all unhandled exceptions, try specifying exception type (or generics). If you catch can't handle specified exception type throw up occurs.

try
{
    bool enabled = ((DataTable)gttBindingSourceLosplaatsen.DataSource).Rows.Count > 0;
    buttonEditLosplaats.Enabled = enabled;
    buttonDeleteLosplaats.Enabled = enabled;
}
catch (Exception ex)
{
    MessageBox.Show("what is this ?");
}

I solved my problem by changing my code to this :

gttTextBoxLotnr.Focus();
this.ActiveControl = gttTextBoxLotnr;

bool enabled = ((DataTable)gttBindingSourceLosplaatsen.DataSource).Rows.Count > 0;
buttonEditLosplaats.Enabled = enabled;
buttonDeleteLosplaats.Enabled = enabled;

Now the exception "Index -1 does not have a value" does not occurs anymore and I have no need anymore for the try catch. buttonDeleteLosplaats had the Focused property = true and that somehow caused the exception when setting the enabled property to false.

That still leaves me with 2 questions wich will probably never be answered:

  1. I still do not know why my catch clause was not reached
  2. Setting the enabled property to false for a focused control throws an exception, why is that needed ? I cannot think of any condition where this is serious enough to throw an exception for, the form should just be left with no active control, that's it, no harm done, no problem anywhere in any universe, so why throw an exception ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM