处理异常注意事项

 
  1. 不要用Exception决定流程。抛异常会消耗系统资源。(if, return null)
  2. Throw exceptions instead of returning an error code or HRESULT.
  3. 不要catch( Exception ex)
  4. 何时扑获异常
    1. Gather information for logging
    2. Add any relevant information to the exception
    3. Execute cleanup code
    4. Attempt to recover
  5. catch块要从特殊到一般排列
  6. catch中抛出的异常不能被同级的catch扑获
  7. 当异常无法恢复时回滚部分完成的操作
  8.           异常层次
ApplicationException
      UIException
      BllException
                  FailedToLoadUserInfoException
      DalException(也许不用)
  1. 我们什么时候需要一个新的Application Exception
    1. Does an exception exist for this condition?
    2. Does a particular exception need explicitly handling?
    3. Do you need specific behavior or additional information for a particular exception?
  2. 如何声明一个异常
 
using System;
using System.Runtime.Serialization;
 
namespace CompanyName.ProductionName.Exceptions
{
        ///<summary>
        ///
        ///</summary>
        [Serializable
        public class BusinessLayerException : ApplicationException
        {
                ///<summary>
                /// Default constructor
                ///</summary>
                public BusinessLayerException() : base()
                {
                }
 
                ///<summary>
                /// Initializes with a specified error message.
                ///</summary>
                ///<param name="message">A message that describes the error.</param>
                public BusinessLayerException(string message) : base(message)
                {
                }
 
                ///<summary>
                /// Initializes with a specified error
                /// message and a reference to the inner exception that is the cause of this exception.
                ///</summary>
                ///<param name="message">The error message that explains the reason for the exception.
                ///</param>
                ///<param name="exception">The exception that is the cause of the current exception.
                /// If the innerException parameter is not a null reference, the current exception
                /// is raised in a catch block that handles the inner exception.
                ///</param>
                public BusinessLayerException(string message, Exception exception) :
                        base(message, exception)
                {
                }
 
                ///<summary>
                /// Initializes with serialized data.
                ///</summary>
                ///<param name="info">The object that holds the serialized object data.</param>
                ///<param name="context">The contextual information about the source or destination.
                ///</param>
                protected BusinessLayerException(SerializationInfo info, StreamingContext context) :
                        base(info, context)
                {
                    _machineName = info.GetString("_machineName");
                }
 
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("_machineName", _machineName, typeof(string));
            base.GetObjectData(info, context);
        }
 
        private string _machineName = Environment.MachineName;
 
        public string MachineName
        {
            get
            {
                return _machineName;
            }
            set
            {
                _machineName = value;
            }
        }
        }
}
Do not derive user-defined exceptions from the Exception base class.
End exception class names with the word "Exception".
You should add fields to this base class to capture specific information, such as the date and time the exception occurred, the machine name, and so on. This encapsulates the common exception details into the single base class and makes this information available to your exception classes through inheritance.
  1. 所有异常和实体定义在单独的程序集中
  2. Throw an InvalidOperationException if a property set or method call is not appropriate given the object's current state.
  3. Throw an ArgumentException or a class derived from ArgumentException if invalid parameters are passed.
    1. ArgumentNullException whenever a null reference (Nothing in Visual Basic) is passed to a method that does not accept it as a valid argument.
    2. ArgumentOutOfRangeException when the value of an argument is outside the range of acceptable values; for example, when the value "46" is passed as the month argument during the creation of a DateTime.
  4. Use exception builder methods.
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值