当前位置:   article > 正文

处理异常注意事项_businesslayer exception

businesslayer exception
 
  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.
 
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/891895
推荐阅读
相关标签
  

闽ICP备14008679号