途径的错误code /信息管理.NET [英] Approaches for Error Code/Message Management in .NET

查看:82
本文介绍了途径的错误code /信息管理.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找管理上的错误codeS和信息,多层应用程序的建议/最佳实践。特别是像:

Looking for suggestions/best practices on managing error codes and messages in a multi-tiered applications. Specifically things like:

  1. 应该在哪里出错codeS界定?枚举?类?
  2. 如何为错误信息或错误codeS相关的更多细节?资源文件?属性的枚举值,等等?
  3. 如果您有一个多层应用程序,包括DAL,BLL,UI的,例如常见的项目,应该有C $ CS的所有层级巨单的$列表,或者是codeS扩展由项目/层?

更新:重要一提的是,我不能仅仅依靠异常和自定义异常类型的错误报告,因为某些客户端这个应用程序将通过Web服务(SOAP和放大器; REST)

Update: Important to mention that I can't rely solely on Exceptions and custom Exception types for error reporting, as some clients for this application will be via web services (SOAP & REST)

任何建议,欢迎!

推荐答案

错误codeS老斯库尔,你需要他们回来的COM和C ++编程,即不支持异常环境的坏日子。如今,你使用异常,通知客户端code或有关问题的用户。在.NET异常是自描述性的,他们有一个类型,信息和诊断。

Error codes are old skool, you needed them back in the bad old days of COM and C programming, environments that didn't support exceptions. Nowadays, you use exceptions to notify client code or the user about problems. Exceptions in .NET are self-descriptive, they have a type, a message and diagnostics.

您需要区分两种类型的异常,那些合理的恢复的,那些你不知道究竟哪里出了问题,或你如何从中进行恢复。后一种是目前最常见的,你需要一个人采取纠正措施。使用内置的.NET异常类型的标准之一,以提高他们。 IllegalOperationException,出现FormatException,ArgumentException的是常见的选择。如果是后端引发这样的例外,那么你只是想通过它传递不改变。您通常需要一个finally块,以确保您的内部状态是一致的,有时会包含一个简单的抛来恢复状态的catch块。

You need to distinguish two types of exceptions, those that are reasonably recoverable and those where you have no idea what exactly went wrong or how you recover from them. The latter kind is by far the most common, you'll need a human to take corrective action. Use one of the standard built-in .NET exception types to raise them. IllegalOperationException, FormatException, ArgumentException are common choices. If it is the back-end that raises such an exception then you just want to pass it through without altering. You typically need a finally block to ensure your internal state is consistent, sometimes a catch block that contains a simple throw to recover state.

任何你认为可收回应该提高与声明自己的异常类型,从Exception类派生。这给了code上游写一个catch子句,做一些有意义的机会。你需要生成一条消息,是描述性的问题,以防上游code实际上并不处理它,需要消息的文本进行,无论是从硬件codeD检索字符串或资源,如果你支持本地化。

Anything you consider recoverable should be raised with an exception type that you declare yourself, derived from the Exception class. That gives code upstream a chance to write a catch clause and do something meaningful. You'll need to generate a Message that is descriptive of the problem, in case the up-stream code doesn't actually handle it, the text of the message needs to be retrieved from either a hard-coded string or a resource if you support localization.

这篇关于途径的错误code /信息管理.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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