1

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 ?

12
  • Which exception? Post the complete exception. Change your Show to MessageBox.Show(e.ToString()) then post the result. Commented Apr 15, 2015 at 15:16
  • have you tried using catch (Excetion e)? Commented Apr 15, 2015 at 15:17
  • You forgot 2 more: AppDomain.CurrentDomain.UnhandledException and main itself (its code should reside inside try/catch as well). Commented Apr 15, 2015 at 15:17
  • The exception is "Index -1 does not have a value" but it does not matter, the question is why do I not fall into the catch block. I also tried using catch (Exception e) but still does not work Commented Apr 15, 2015 at 15:19
  • 1
    Well the exception does matter, because Index -1 does not have a value tells us the exception occurred somewhere else after you set the button the enabled, and that somewhere else most likely doesn't catch the exception. Commented Apr 15, 2015 at 15:25

3 Answers 3

6

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); } 
Sign up to request clarification or add additional context in comments.

3 Comments

I implemented your code and now I do indeed make it into the catch as I expected. One thing I found strange is that other try catch blocks in this application did work as I expected without this fix. Also it is not clear to me how this works when the application is run standalone, will it get into the catch block or not ?
No, that is not the expected outcome. You can't expect me to theorize about what is going on with this little information.
I have tested the application standalone, and the problem is back. The catch block is not reached and the exception is handled by the global exception handler so now I am still stuck here. All I want to do is supress the exception message. Setting up an empty catch block seemed like the answer but that does not work, so is there another way I can accomplish this ? As you already said, finding the code that throws the exception seems impossible
0

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 ?"); } 

1 Comment

Tried this, same result
0

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 ?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.