在 WPF 应用程序中全局捕获异常? [英] Globally catch exceptions in a WPF application?

查看:23
本文介绍了在 WPF 应用程序中全局捕获异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个 WPF 应用程序,它的一部分可能会在运行时抛出异常.我想全局捕获任何未处理的异常并记录它们,否则继续执行程序,就好像什么都没发生一样(有点像 VB 的 On Error Resume Next).

We are having a WPF application where parts of it may throw exceptions at runtime. I'd like to globally catch any unhandled exception and log them, but otherwise continue program execution as if nothing happened (kinda like VB's On Error Resume Next).

这在 C# 中可行吗?如果是这样,我究竟需要将异常处理代码放在哪里?

Is this possible in C#? And if so, where exactly would I need to put the exception handling code?

目前我看不到任何可以将 try/catch 包裹起来并捕获所有可能发生的异常的点.即便如此,我也会因为抓到而离开已经执行的任何东西.还是我在这里的想法非常错误?

Currently I can't see any single point where I could wrap a try/catch around and which would catch all exceptions that could occur. And even then I would have left whatever has been executed because of the catch. Or am I thinking in horribly wrong directions here?

ETA: 因为下面很多人指出:该应用程序不是用于控制核电站.如果它崩溃了,那没什么大不了的,但主要与 UI 相关的随机异常在使用它的上下文中会令人讨厌.有(并且可能仍然是)其中一些,因为它使用插件架构并且可以由其他人扩展(在这种情况下也是学生;所以没有有经验的开发人员能够完全编写错误- 免费代码).

ETA: Because many people below pointed it out: The application is not for controlling nuclear power plants. If it crashes it's not that much a big deal but random exceptions that are mostly UI-related are a nuisance in the context where it would be used. There were (and probably still are) a few of those and since it uses a plugin architecture and may be extended by others (also students in that case; so no experienced developers that are able to write completely error-free code).

至于被捕获的异常:我确实将它们记录到日志文件中,包括完整的堆栈跟踪.这就是该练习的全部意义所在.只是为了反驳那些将我的类比比喻为 VB 的 OERN 的人过于字面化.

As for the exceptions that get caught: I do log them to a log file, including the complete stack trace. That was the whole point of that exercise. Just to counter those people that were taking my analogy to VB's OERN too literally.

我知道盲目忽略某些类别的错误是危险的,并且可能会损坏我的应用程序实例.如前所述,该程序对任何人都不是关键任务.没有人会以人类文明的生存为赌注.它只是一个用于测试某些设计方法的小工具.软件工程.

I know that blindly ignoring certain classes of errors is dangerous and might corrupt my application instance. As said before, this program isn't mission-critical for anyone. No-one in their right mind would bet the survival of the human civilization on it. It's simply a little tool for testing certain design approaches wrt. software engineering.

为了立即使用应用程序,异常可能发生的事情并不多:

For the immediate use of the application there are not many things that can happen on an exception:

  • 无异常处理——错误对话框和应用程序退出.实验必须重复,尽管可能是另一个主题.没有记录任何错误,这是不幸的.
  • 通用异常处理——良性错误被捕获,无害.这应该是从我们在开发过程中看到的所有错误来判断的常见情况.忽略此类错误应该不会立即产生后果;核心数据结构经过充分测试,可以轻松应对.
  • 通用异常处理 - 捕获严重错误,可能在稍后崩溃.这可能很少发生.到目前为止,我们从未见过它.无论如何都会记录错误,并且崩溃可能是不可避免的.所以这在概念上类似于第一种情况.除了我们有一个堆栈跟踪.在大多数情况下,用户甚至不会注意到.

至于程序生成的实验数据:严重的错误最坏的情况是导致没有数据记录.微小的改变几乎不可能改变实验结果.即使在这种情况下,如果结果看起来可疑,就会记录错误;如果它完全是异常值,人们仍然可以丢弃该数据点.

As for the experiment data generated by the program: A serious error would at worst just cause no data to be recorded. Subtle changes that change the result of the experiment ever so slightly are pretty unlikely. And even in that case, if the results seem dubious the error was logged; one can still throw away that data point if it's a total outlier.

总结:是的,我认为我自己至少还是部分理智的,我不认为全局异常处理例程会使程序运行必然是完全邪恶的.如前所述,这种决定可能是有效的,具体取决于应用程序.在这种情况下,它被认为是一个有效的决定,而不是彻头彻尾的胡说八道.对于任何其他应用程序,该决定可能看起来不同.但是请不要指责我或参与该项目的其他人仅仅因为我们忽略了错误而可能炸毁世界.

To summarize: Yes, I consider myself still at least partially sane and I don't consider a global exception handling routine which leaves the program running to be necessarily totally evil. As said twice before, such a decision might be valid, depending on the application. In this case it was judged a valid decision and not total and utter bullshit. For any other application that decision might look different. But please don't accuse me or the other people who worked on that project to potentially blow up the world just because we're ignoring errors.

旁注:该应用程序只有一个用户.它不像 Windows 或 Office 那样被数以百万计的用户使用,在这些东西中,让异常冒泡给用户的成本从一开始就已经大不相同.

Side note: There is exactly one user for that application. It's not something like Windows or Office that gets used by millions where the cost of having exceptions bubble to the user at all would be very different in the first place already.

推荐答案

使用 Application.DispatcherUnhandledException 事件.请参阅此问题的摘要(请参阅Drew Noakes 的回答).

Use the Application.DispatcherUnhandledException Event. See this question for a summary (see Drew Noakes' answer).

请注意,仍有一些异常会阻止您的应用程序成功恢复,例如在您尝试保存到数据库时出现堆栈溢出、内存耗尽或网络连接丢失等情况.

Be aware that there'll be still exceptions which preclude a successful resuming of your application, like after a stack overflow, exhausted memory, or lost network connectivity while you're trying to save to the database.

这篇关于在 WPF 应用程序中全局捕获异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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