Microsoft Enterprise Library中的Exception Handle模块主要用来处理程序中的异常错误,它的实现原理为遮罩模式:不让敏感的信息泄露。类似Logging模块,我们也可以通过Configuration程序便捷的对它进行配置。
Exception Handle模块默认为我们提供了多种异常处理功能:Wrap Handler,Replace Handler,Logging Handler,Fault Contract Exception Handler,Custom Exception Handler。在日常应用中,我们都会将它们组合使用,发挥它们的整体效果。它们的功能分别为:
Wrap Handler:包装原始错误,以达到过滤敏感数据的效果。
Replace Handler:替换原始错误,并给出一个新的错误。
Logging Handler:纪录原始错误的相应信息。
Fault Contract Exception Handler:捕捉具体的协议错误,该异常处理功能主要用在WCF Service上。
Custom Exception Handler:自定义错误处理模块,也就是说我们可以实现自己的错误处理程序。
Wrap Handler
//Warp Exception
try
{
int a = 10;//int.Parse(Console.ReadLine());
int b = 0;//int.Parse(Console.ReadLine());
int c = a / b;
Console.WriteLine(string.Format("{0}/{1}={2}", a, b, c));
}
catch (Exception ex)
{
Exception exceptionToRethrow;//=new Exception("what's up");
ExceptionManager exManager = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
bool rethrow = exManager.HandleException(ex, "Divide Exceptions", out exceptionToRethrow);
if (rethrow)
{
Console.WriteLine(exceptionToRethrow.Message);
}
}
配置信息
Replace Handler + Logging Exception Handler
//Replace Exception
try
{
throw new System.Data.DataException("abc");
}
catch (Exception ex)
{
Exception exceptionToRethrow;
ExceptionManager exManager = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
bool rethrow = exManager.HandleException(ex, "DB Exceptions", out exceptionToRethrow);
if (rethrow)
{
throw exceptionToRethrow;
}
}
配置信息
Fault Contract Exception Handler
try
{
WCF.HelloClient client = new WCF.HelloClient();
client.DoWork();
}
catch (FaultException ex)
{
var faultDetail = ex.CreateMessageFault();
SimpleExceptionHandlerDemoInWCF.FaultMessage faultMessage=null;
if (faultDetail.HasDetail)
{
faultMessage=faultDetail.GetDetail<SimpleExceptionHandlerDemoInWCF.FaultMessage>();
}
if (faultMessage != null)
{
Console.WriteLine(string.Format("ID:{0}|Message:{1}|Detail:{2}",faultMessage.ID,faultMessage.Message,ex.Message));
}
}
WCF Service代码
[ServiceContract]
public interface IHello
{
[OperationContract]
FaultMessage DoWork();
}
[ExceptionShielding("wcfexception")]
public class Hello : IHello
{
public FaultMessage DoWork()
{
throw new NotImplementedException();
}
}
[DataContract]
public class FaultMessage
{
[DataMember]
public Guid ID{get;set;}
[DataMember]
public string Message{get;set;}
}
配置信息
Custom Exception Handler
//Test custom exception handler
try
{
throw new Exception("ok");
}
catch (Exception ex)
{
Exception latestException = null;
ExceptionManager exManager = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
bool reThrow = exManager.HandleException(ex, "customexceptionhandler", out latestException);
}
Custom Exception Handler 实现代码
namespace SimpleExceptionHandlerDemo
{
using System;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
/// <summary>
/// TODO: Update summary.
/// </summary>
///
[ConfigurationElementType(typeof(CustomHandlerData))]
public class MessageBoxCustomExceptionHandler:IExceptionHandler
{
public MessageBoxCustomExceptionHandler(NameValueCollection keys)
{ }
public Exception HandleException(Exception exception, Guid handlingInstanceId)
{
MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return exception;
}
}
}
配置信息
小结
一个错误类型能具有多个错误处理者,可以参考Replace Exception Handler+Logging Exception Handler小节,系统是按从上至下的顺序进行处理的。所以在这里Logging Exception Handler记录的错误信息是被Replace Exception Handler处理后的信息。