菩提树的Framework架构

.net 架构,数据库设计,编码规范

导航

(Exception handle)分层架构异常处理机制

针对于通常的三层架构 我们需要针对于异常做一些处理,从上面的异常处理组件图当中,我们能看到自定义异常和异常处理层层上抛,具体我们还是来看代码吧!

自定义异常:

 

 /// <summary>
    /// 自定义BLL层异常类
    /// </summary>
    [Serializable]
    public class BLLException : Exception, ISerializable
    {
        private string myBLLMsg;
        /// <summary>
        /// 自定义本地文本信息属性
        /// </summary>
        public string MyBLLMsg
        {
            get
            {
                return myBLLMsg;
            }
        }
        /// <summary>
        /// 重写只读本地文本信息属性
        /// </summary>
        /// <remarks>
        /// 在myExcuteMsg不存在的情况下,返回继承Base.Message,
        /// 如果存在则返回 myExcuteMsg + base.Message
        /// </remarks>
        public override string Message
        {
            get
            {
                return myBLLMsg == null ? base.Message : myBLLMsg + base.Message;
            }
        }
        /// <summary>
        /// 实现基类的各公用构造函数
        /// </summary>
        public BLLException()
            : base() { }
        /// <summary>
        /// 实现基类的各公用构造函数
        /// </summary>
        /// <param name="message">基类的异常消息</param>
        public BLLException(string message)
            : base(message) { }
        /// <summary>
        /// 实现基类的各公用构造函数
        /// </summary>
        /// <param name="messager">基类的异常消息</param>
        /// <param name="innerException">导致当前异常的异常</param>
        public BLLException(string messager, Exception innerException)
            : base(messager, innerException) { }
        /// <summary>
        /// 实现自定义消息公用构造函数
        /// </summary>
        /// <param name="message">基类的异常消息</param>
        /// <param name="myExcuteMsg">自定义异常信息</param>
        public BLLException(string message, string myExcuteMsg)
            : base(message)
        {
            this.myBLLMsg = myExcuteMsg;
        }
        /// <summary>
        /// 实现自定义消息公用构造函数
        /// </summary>
        /// <param name="messager">基类的异常消息</param>
        /// <param name="myExcuteMsg">自定义异常信息</param>
        /// <param name="innerException">导致当前异常的异常。</param>
        public BLLException(string messager, string myExcuteMsg, Exception innerException)
            : base(messager, innerException)
        {
            this.myBLLMsg = myExcuteMsg;
        }

        protected BLLException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            myBLLMsg = info.GetString("MydelMsg");
        }
        /// <summary>
        /// 重写GetObjectData方法,实现向SerializationInfo中添加自定义字段信息。
        /// </summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("MydelMsg", myBLLMsg);
            base.GetObjectData(info, context);
        }
    }

 

DAC层异常处理:

 

 public class BaseDataHelper : IBaseDataHelper
    {

 

        #region IBaseDataHelper 成员

        public System.Data.DataSet BaseQuery(string strSql)
        {
            try
            {
                return DbHelperOra.Query(strSql);
            }
            catch (DACException ex)
            {
                throw new DACException("数据库访问错误", "查询公用模块错误", ex);
            }
        }

        #endregion

 

        #region IBaseDataHelper 成员


        public int ExecuteSql(string strSql)
        {
            try
            {
                return DbHelperOra.ExecuteSql(strSql);
            }
            catch (DACException ex)
            {
                throw new DACException("数据库访问错误", "执行SQL语句出错", ex);
            }
        }
        public bool ExecuteSqlTran(ArrayList SQLStringList)
        {
            try
            {
                DbHelperOra.ExecuteSqlTran(SQLStringList);
            }
            catch (DACException ex)
            {
                throw new DACException("数据库访问错误", "执行事务出错", ex);
            }
            return true;
        }

        public bool ExecuteSqlTran(Hashtable SQLStringList)
        {
            try
            {
                DbHelperOra.ExecuteSqlTran(SQLStringList);
            }
            catch (DACException ex)
            {
                throw new DACException("数据库访问错误", "执行事务出错", ex);
            }
            return true;
        }
        #endregion

        #region IBaseDataHelper 成员


        public object GetSingle(string strSql)
        {
            try
            {
                return DbHelperOra.GetSingle(strSql);
            }
            catch (DACException ex)
            {
                throw new DACException("数据库访问错误", "获取单个对象出错", ex);
            }
        }

        #endregion
    }

BLL异常处理:

 

public class UserBLL:IUserManager
    {
        #region IUserManager 成员

        public System.Data.DataTable GetUserDataTable()
        {
            try
            {
                string strsql = "select * from napp_accounts_user ";
                return DataAccess.CreateBaseDataHelper().BaseQuery(strsql).Tables[0];
            }
            catch (BLLException ex)
            {
                throw new BLLException("查询用户业务", ex);
            }
        }

        #endregion
    }

 

 

界面异常处理:

log4net.ILog logger = log4net.LogManager.GetLogger(typeof(_Default));
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            DataTable dt = BLLAccess.CreateUserBLL().GetUserDataTable();
            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();
        }
        catch(Exception ex)
        {
            //log4net记录异常错误
            logger.Error("查询用户绑定GetUserDataTable() Error:" + ex.ToString());
            //在此可添加返回错误页面
        }
    }

posted on 2009-03-01 12:39  菩提树下  阅读(3779)  评论(16编辑  收藏  举报