.Net Winforms 中未捕获设计时异常

发布于 2024-09-26 05:17:32 字数 348 浏览 2 评论 0原文

我有一个在设计时用于配置各种属性的表单。
我尝试了两种方法来执行表单级捕获所有异常:

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2024-10-03 05:17:32

从技术上讲,您在设计时没有 Application 对象,并且它不会像您那样调用 Show() 方法。捕获异常的最佳方法是用 try/catch 包围设计者在加载时遇到问题的有问题的方法或属性并抛出异常。

只是为了进行测试,请继续向表单添加一个名为 IamGoingToThrowAnException 的虚拟属性,并在该属性的 getter 中执行 throw new System.Exception("Caught me in design mode "),如下所示:

public string IamGoingToThrowAnException 
{
    get
    {
        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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文