在全球范围内赶在WPF应用程序异常? [英] Globally catch exceptions in a WPF application?

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

问题描述

我们正处于一个WPF应用程序,其中的某些部分可能会在运行时抛出异常。我想在全球范围内捕捉任何未处理的异常,并记录它们,但在其他方面继续执行程序,仿佛什么都没有发生(有点像VB的上的错误继续下一步)。

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#这可能吗?如果是这样,正是在那里我会需要把异常处理code?

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

目前我看不到任何单一的地步,我可以换一个尝试 / 四周,这将赶上可能发生的所有异常。即使在那时,我会离开,因为渔获物的任何已执行。还是我这里可怕的错误方向思考?

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相关的是在上下文滋扰哪里会被使用。有(可能仍然是)一些那些而且由于它使用了插件架构,可能由他人(也学生在这种情况下进行扩展,所以的没有的有经验开发人员能够编写完全错误 - 免费code)。

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.

我知道一味地忽略错误某些类别是危险的,可能会损坏我的应用程序实例。正如之前所说,这个计划是不是关键任务的人。没有人在心智打赌就可以了人类文明的生存。它只是用于测试某种设计方法WRT一个小工具。软件工程。

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:


  • 无异常处理 - 错误对话框,并且应用程序退出。实验必须重复,有可能与另一个问题虽然。没有错误已记录,这是不幸的。

  • 通用​​异常处理 - 良性错误所困,又何妨呢。这应该是共同的情况下,从我们的发展过程中看到所有的错误判断。忽略这种错误,应该没有直接的后果;核心数据结构进行测试不够好,他们会很容易生存之本。

  • 通用​​异常处理 - 被困严重的错误,可能崩溃在稍后一点。这可能会很少发生。我们从来没有见过这么远。反正是记录错误和崩溃可能是不可避免的。因此,这在概念上类似于非常第一例。除了我们有一个堆栈跟踪。而在大多数情况下,用户甚至不会注意到。

至于由程序生成的实验数据:一个严重错误将在最糟糕的正义事业要记录任何数据。即改变实验结果的微妙变化非常轻微的pretty可能性不大。而且,即使在这种情况下,如果结果似乎可疑被记录的错误;人们仍然可以扔掉该数据点,如果它是一个总的异常。

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.

推荐答案

使用的<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.application.dispatcherunhandledexception.aspx\"><$c$c>Application.DispatcherUnhandledException事件 。请参见这个问题的摘要(见的德鲁诺克斯的回答)。

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

请注意,还会有还是例外,preclude你的应用的成功恢复时,就像一个堆栈溢出,耗尽内存,或者当你试图保存到数据库中丢失网络连接之后。

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天全站免登陆