如何捕获用户控制错误 [英] How to catch User Control Errors

查看:49
本文介绍了如何捕获用户控制错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个WPF  User Control 这是它自己的.dll。 然后我有一个"主要" WPF应用程序将读取不同的用户控件(通过数据库),然后在选项卡中显示它们。 我向用户控件添加了错误处理,但
显示为"Main"。 WPF应用程序正在捕获所有内容,只是将它们显示为未处理,甚至没有将我的错误记录到数据库中。 我是否需要在我的用户控制项目中执行某些特定操作才能使主叫"主要"
WPF应用程序使用我的用户控件dll进行错误处理?

I made a WPF User Control that is its own .dll.  Then I have a "Main" WPF application that will read in different user controls (via the database) and then show them in a tab.  I added error handling to my user control, but it appears the "Main" WPF application is catching everything and just showing them as un-Handled and not even logging my errors to the database.  Do I need to do something specific in my User Control project to make the calling "Main" WPF application resort to my User Control dll for error handling?

推荐答案

如果没有代码示例,很难知道发生了什么。

This is difficult to know what is going on without code examples.

如果dll中的用户控件抛出异常,则会以与任何其他异常完全相同的方式处理这些异常(冒泡调用链直到遇到catch块或触发应用程序的未处理的异常事件)。

If the user control in the dll throws exceptions though, these will be treated in exactly the same way as any other exceptions (bubbling up the call chain until they encounter a catch block or else triggering the application's unhandled exception event).

通常,只有真正捕获异常才能在适当的应用程序"层边界"执行日志记录然后重新抛出,从而导致应用程序崩溃。这是因为异常
只应在特殊情况下抛出,在这种情况下,应用程序不再处于已知的稳定状态。

Generally, exceptions should only really be caught in order to perform logging at an appropriate application 'layer boundary' and then rethrown, hence crashing the app. This is because exceptions should only be thrown in exceptional situations and in this case the app is no longer in a known stable state.

异常如果你别无选择:使用较旧的API然后抛出异常(对于那些本不应该是异常的东西)(比如旧的Int.Parse方法,后来用Int.TryParse改进)或者你需要处理一些不可靠的进程你的应用程序之外的
,例如互联网访问,文件处理等,如果出现问题,只需重试或放弃就可以接受。

The exceptions to this are if you have no choice: using an older API then throws exceptions for things that shouldn't really be exceptions (like the old Int.Parse method, later improved with Int.TryParse) or if you need to handle some unreliable process outside of your app such as internet access, file handling and so on where it may be acceptable to simply retry or give up gracefully if something goes wrong.

(我会请您参考我的
最喜欢的文章
)。

(I'll refer you to my favourite article on this kind of thing).


这篇关于如何捕获用户控制错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆