views:

79

answers:

1

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?

+4  A: 

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
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
@willem: You might try using `Application.UnhandledException` - that *might* do it...
Jon Skeet
Thanks, I'll try that
willem