2

I have the following code in my project, deleteselector is a form that has a datagridview (with autosize columns) on it.

try
{
      if (deleteSelector.ShowDialog() == DialogResult.OK)
      {
      }
}
catch (InvalidOperationException)
{
   //Bug workaround
}

The try catch is because a pop-up form with a gridview on it trows a invalidoperationexception once in a while. This is the suggested workaround, see

http://connect.microsoft.com/VisualStudio/feedback/details/145633/invalidoperationexception-thrown-when-a-form-with-a-bound-datagridview-with-auto-sizing-columns-is-shown

Earlier, I used Show on the deleteSelector, and the workaround worked perfectly. Now, with showdialog it seems that the error is not catched anymore (I get an uncatched error message). Why is the error not catched?

spender
  • 117,338
  • 33
  • 229
  • 351
willem
  • 2,617
  • 5
  • 26
  • 38

1 Answers1

4

ShowDialog runs the dialog on a separate thread, so the exception is being thrown in a different stack to your exception handler.

I suggest you try to find a different workaround - just catching InvalidOperationException is pretty horrible, and I wouldn't like to bet that the form would be in a sensible state afterwards.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Is there a way to catch this exception then? A lot of people tried to find a workaround, and this is what they suggested in the end. The workaround worked fine until I decided to use ShowDialog instead of show – willem Sep 17 '10 at 09:08
  • 2
    @willem: You might try using `Application.UnhandledException` - that *might* do it... – Jon Skeet Sep 17 '10 at 09:17