.Net Winforms 中未捕获设计时异常
我有一个在设计时用于配置各种属性的表单。
我尝试了两种方法来执行表单级捕获所有异常:
(1) 在构造函数中向 Application.ThreadException 添加一个处理程序。
(2) 我将表单的 Show 方法包装在 Try/Catch 块中
当我通过向表单添加属性网格并将我的组件设置为 SelectedObject 进行测试时,这两个方法都在运行时工作。
然而,在设计时,表单只是简单地关闭,没有任何消息;我的消息和任何解释存在未处理异常的消息都没有。
有什么想法吗?
ETA:这与设计时的调试无关。这是关于当我的类型编辑器遇到未处理的异常时如何向用户呈现友好的消息。
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 a Try/Catch block
Both 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从技术上讲,您在设计时没有
Application
对象,并且它不会像您那样调用Show()
方法。捕获异常的最佳方法是用 try/catch 包围设计者在加载时遇到问题的有问题的方法或属性并抛出异常。只是为了进行测试,请继续向表单添加一个名为
IamGoingToThrowAnException
的虚拟属性,并在该属性的 getter 中执行throw new System.Exception("Caught me in design mode ")
,如下所示:同样的操作适用于通过
InitializeComponents
方法或表单的构造函数初始化的任何方法或属性。You don't have an
Application
object at design-time technically, and it doesn't call theShow()
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 athrow new System.Exception("Caught me in design mode")
, as follows:The same thing works for any methods, or properties that are initialized through the
InitializeComponents
method, or constructor of your form.