I have a form that is used at design-time to configure various properties.
I've tried two ways to do a form-level catch all exception:(1) I add a handler to Application.ThreadException in the constructor.
(2) I wrap the Show method, of the form, in 开发者_C百科a Try/Catch blockBoth of these work at run-time when I test by adding a property grid to a form and set my component as the SelectedObject.
However, at design-time the form simply closes with no message whatsoever; neither my message nor any message explaining that there is an unhandled exception.
Any ideas?
ETA: This is not about debugging at design-time. It's about how to present the user with a friendly message when my type editor encounters an unhandled exception.
You don't have an Application
object at design-time technically, and it doesn't call the Show()
method the same way you do. The best way to catch the exception is to surround the offending method or property that the designer is having a problem loading with a try/catch and throw the exception.
Just for a test, go ahead and add a dummy property to your form, called IamGoingToThrowAnException
, and in the getter for the property do a throw new System.Exception("Caught me in design mode")
, as follows:
public string IamGoingToThrowAnException
{
get
{
throw new System.Exception("Caught me in design mode.");
}
}
The same thing works for any methods, or properties that are initialized through the InitializeComponents
method, or constructor of your form.
精彩评论