全球处理例外的窗口服务 [英] Global handling exception in window service

查看:100
本文介绍了全球处理例外的窗口服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经运行的服务器的Windows服务。但是,服务器有时通过一个错误,是联合国处理立即停止。请帮我该如何处理全局异常。谢谢你。

解决方案

这听起来像你对我正在试图解决这个问题的错误的方式围绕...

当你的程序(或服务)崩溃,因为未处理的错误,解决的办法是不弄清楚在何处和如何处理的所有的未处理的错误,这样就可以忽略它们并继续执行。我已经散了视野更清晰<一href="http://stackoverflow.com/questions/4827628/main-method-$c$c-entirely-inside-try-catch-is-it-bad-practice/4827646#4827646">in这个答案,但短期的版本是,你遇到未处理的异常时,正确的做法是崩溃。由于报价在原来的答案:

  

发生了未处理的异常,这一事实意味着服务器是在一个意想不到的状态。通过捕捉异常,说:别担心,这一切都很好,你最终留下一个损坏的服务器上运行。

     

[。 。 。 ]

     

捕获所有异常,并让这个过程继续运行假设一台服务器可以从意外故障中恢复。但是,这是荒谬的。你已经知道服务器是无法恢复的敬酒:坠毁

     

好多是让服务器崩溃,这样的崩溃转储可以在故障点被捕获。现在你搞清楚是怎么回事的一个战斗的机会。

所以,事实上,在的真正的解决方案涉及摸清了未处理的异常的根本原因,并从第一个地方发生的修改你的code至prevent的错误。

有没有办法,我们可以帮你做到这一点,除非你发布的确切异常消息,你要和preferably一个完整的堆栈跟踪。但是你一定要preserve你得到,而不是想出了一个办法,完全与mdash忽略它的调试信息,这是真正的解决的问题的唯一途径。

如果你仍然坚持忽略所有善意的建议,相反,你会发现坚持你的头在沙中并忽略它的做法<一个href="http://stackoverflow.com/questions/1682128/global-exception-handler-for-windows-services">detailed这里。

I have a windows service run as a server. However, the server sometime stops immediately by an error which is un-handled. Please help me how to handle global exception. Thanks.

解决方案

It sounds to me like you're trying to solve the problem the wrong way around...

When your program (or service) crashes because of an unhandled error, the solution is not to figure out where and how to "handle" all unhandled errors so that you can ignore them and continue execution. I've hashed out that view more clearly in this answer, but the short version is that when you encounter an unhandled exception, the correct thing to do is to crash. As quoted in the original answer:

The fact that an unhandled exception occurred means that the server was in an unexpected state. By catching the exception and saying, "Don't worry, it's all good," you end up leaving a corrupted server running.

[ . . . ]

Catching all exceptions and letting the process continue running assumes that a server can recover from an unexpected failure. But this is absurd. You already know that the server is unrecoverably toast: It crashed!

Much better is to let the server crash so that the crash dump can be captured at the point of the failure. Now you have a fighting chance of figuring out what's going on.

So in fact, the real solution involves figuring out the root cause of the unhandled exception and modifying your code to prevent that error from occurring in the first place.

There's no way that we can help you to do that unless you post the exact exception message that you're getting, and preferably a full stack trace. But you definitely want to preserve the debugging information that you're getting, rather than come up with a way to ignore it entirely—that's the only way to actually solve the problem.

If you still insist on ignoring all well-meaning advice to the contrary, you'll find the "stick your head in the sand and ignore it" approach detailed here.

这篇关于全球处理例外的窗口服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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