針對於通常的三層架構 我們需要針對於異常做一些處理,從上面的異常處理組件圖當中,我們能看到自定義異常和異常處理層層上拋,具體我們還是來看代碼吧!
自定義異常:
/// <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());
//在此可添加返回錯誤頁面
}
}