长时间运行的App,如何处理错误?
我必须实现一个信息终端。我选择 dot.net,终端只有一个触摸板。 所以这个系统7天24小时运行。
所以我调用一个网络服务,显示数据,显示网站内容。很多事情都可能出错。
您对这种情况有一些建议吗? try catch 中的每个函数? AppDomain.CurrentDomain.UnhandledException 事件?
谢谢安德烈亚斯
i have to implement a Info Terminal. I choose dot.net and the terminal is only a touchpad.
So this System running 7 days 24 hours.
So i call a Webservice, display Data, show Website stuff. Many things can going wrong.
Have you some recommendations for this scenario?
Every function in an try catch? AppDomain.CurrentDomain.UnhandledException event?
Thanks Andreas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本上,您应该尽快处理任何错误 - 因此,如果您正在调用 webservice,请将所有调用包装在 try/catch 块中并在那里处理错误 - 例如,您可以记录确切的错误,聚合许多错误更通用的 DataSourceFaultException(名称仅用于示例)中与 webservice 相关的异常,然后 UI 将收到该异常,并且 UI 将能够轻松确定它无法显示请求的信息,因为通信失败,并选择重试,通知用户或执行其他操作。
然而,对于长时间运行的应用程序,您将不得不处理更多错误。其中许多并不容易预测,因为它们不一定与任何特定调用相关 - 您可能会耗尽内存,递归可能会导致堆栈溢出,系统计时器可以达到其最大值并从头开始等等。
您不应该在每个方法中处理这些错误,因为它只会损害代码的可读性并且容易出错。这些错误最好由 UnhandledException 事件处理。但是,您必须记住,当异常到达 UnhandledException 事件时,您无法假设有关应用程序状态的任何信息 - 该错误可能已损坏部分(甚至全部)内部状态。因此,当发生这种情况时,最好尝试创建错误日志并正常重新启动应用程序(不一定是整个应用程序 - 也许可以重新初始化应用程序的状态 - 如果是这样,这也是一个有效的选项。但是,您必须意识到您将无法从某些错误中恢复并无论如何处理这种情况)。
Basically, you should handle any error as soon as it is possible - so if you're calling webservice wrap all the calls in a try/catch block and handle the error there - you can, for example, log the exact error, aggregate many webservice-related exception in more generic, DataSourceFaultException (name is only for example), which will be then received by UI and UI will be able to easily determine, that it can't display requested info because communication failed, and choose to retry, notify user or do anything else.
However - with long running application there are many more errors you'll have to deal with. Many of them are not easy to predict, as they're not necessarily related to any specific call - you can run out of memory, a recursion might cause stack overflow, a system timer can reach it's max value and start from the beginning etc.
You shouldn't handle those errors in every method, as it will only hurt code readibility and will be error prone. Those errors are best handled by UnhandledException event. However, you must remember, that when the exception reaches UnhandledException event, you cannot assume anything about state of your application - the error might have corrupted some (or even all) of internal state. So when such condition occurs, it's best to try to create an error log and gracefuly restart the application (not necessarily whole application - maybe it will be possible to reinitialize application's state - if so, that's a valid option too. However, you must be aware that you won't be able to recover from some errors and handle such situation anyway).
这取决于。
如果您可以适当地处理函数内的异常 - 处理它。如果没有 - 创建一个全局异常处理程序来通知用户或记录它。
It depends.
If you can handle appropriately an exception within a function - handle it. If not - create a global exception handler to inform user or log it.