描述:这个程序演示了异常在两个项目中的传播。
项目的静态结构图:
![](/images/cnblogs_com/xuanfeng/L.JPG)
添加项目
、1.打开VS2005 ,新建一个名为ExceptionStudy的解决方数,并添加一个名为QuickStartExcutable的Window应用程序项目。
2.添加一个名为BusinessLayerDLL类库项目。
添加引用
3.为解决方案添加以下相关的Enterprise Library DLL(放在安装目录下的bin 文件夹中),把它们复制到QuickStartExcutable项目的bin\Debug目录下。
. Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll
. Microsoft.Practices.ObjectBuilder.dll
. Microsoft.Practices.EnterpriseLibrary.Common.dll
4.在QuickStartExcutable项目中添加引用,浏览到上面三个DLL,项目里添加BusinessLayerDLL.DLL,.NET里添加System.Configuration 一共五个引用;
在BusinessLayerDLL项目里添加
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll
Microsoft.Practices.EnterpriseLibrary.Common.dll
的引用。
添加异常处理
5.在BusinessLayerDLL类库项目中添加一个Servies类新文件,并在其中添加两个办法来实现引发异常与处理异常情况。
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
![](/Images/OutliningIndicators/None.gif)
namespace BusinessLayerBLL
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public class Servies
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public Servies()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
private void ProcessA()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//在这个线程里发生异常
throw new System.Exception("Original Exception: Fatal exception in business layer");
}
public bool ProcessWithPropagate()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ // 引发异常;
this.ProcessA();
}
catch (Exception ex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//Propagate Policy异常策略声明会再一次抛出异常在此不做处理,返回True值
bool rethrow = ExceptionPolicy.HandleException(ex, "Propagate Policy");
![](/Images/OutliningIndicators/InBlock.gif)
if (rethrow)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
return true;
}
}
}
![](/Images/OutliningIndicators/None.gif)
6.在QuickStartExcutable项目中添加一个AppMessageExceptionHandler的新文件,来实现自定义的异常处理,其要必须符合两个条件:
A.实现继承IExceptionHandler接品口
B.类声明 [ConfigurationElementType(typeof(CustomHandlerData))]属性
using System.IO;
using System;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using System.Collections.Specialized;
![](/Images/OutliningIndicators/None.gif)
namespace QuickStactExcutable
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
[ConfigurationElementType(typeof(CustomHandlerData))]
public class AppMessageExceptionHandler : IExceptionHandler
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public AppMessageExceptionHandler(NameValueCollection ignore)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
//实现IExeptionHandler唯一的一个成员
public Exception HandleException(Exception exception, Guid handlingInstanceId)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//对异常进行处理
DialogResult result = this.ShowThreadExceptionDialog(exception);
![](/Images/OutliningIndicators/InBlock.gif)
return exception;
}
![](/Images/OutliningIndicators/InBlock.gif)
//自定义处理异常的办法,将异常显示出来
private DialogResult ShowThreadExceptionDialog(Exception ex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string errorMsg = "The following exception was caught by the Quick Start Global Exception Handler:" + Environment.NewLine + Environment.NewLine;
![](/Images/OutliningIndicators/InBlock.gif)
StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb);
![](/Images/OutliningIndicators/InBlock.gif)
TextExceptionFormatter formatter = new TextExceptionFormatter(writer,ex);
![](/Images/OutliningIndicators/InBlock.gif)
// Format the exception
formatter.Format();
![](/Images/OutliningIndicators/InBlock.gif)
errorMsg += sb.ToString();
![](/Images/OutliningIndicators/InBlock.gif)
return MessageBox.Show(errorMsg, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
![](/Images/OutliningIndicators/InBlock.gif)
}
}
![](/Images/OutliningIndicators/None.gif)
7.在Form1里添加一个Button名为b_CatchException,添加Button的事件处理的办法如下:
![](/Images/OutliningIndicators/ContractedBlock.gif)
b_CatchException_Click
private void b_CatchException_Click(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Servies servies = new Servies();
servies.ProcessWithPropagate();
}
catch (Exception ex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bool bl = ExceptionPolicy.HandleException(ex,"Global Policy");
if (bl)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Application.Exit();
}
}
![](/Images/OutliningIndicators/InBlock.gif)
}
创建异常处理策略配置文件:
8.打开Enterprise Library Configuration ,右键New Applicaton,设置ConfigurationFile,把它的路径选择在ExceptionStudy存放的目录下,并命名为App
9.右键->New->Exception Handling Application Bock
10.右键->New->Exception Policy,Name为Propagate Policy
8.右键->New->Exception Type ,类型为Exception,PostHandlingAction为NotifyRethrow.
11.重复7,Name为:Global Policy,
12,重复8, PostHandlingAction为None;
13.右键->New->Custom Handler ,设置type ->Load Assembly->浏览到 QuickStartExcutable项目的文件夹,在bin\Debug下选择QuickStartExcutable.exe,点击OK即可。
14.分别在两个项目里,添加现有项,文件类型为所有文件类型,然后找到上面创建的配置文件,添加就行了。
15.到这里整个解决方案就完成了,运行程序就可以啦。点击Button后就可以显示异常的原始信息。
![](/images/cnblogs_com/xuanfeng/EX.JPG)
参考文档ms-help://MS.VSCC.v80/MS.VSIPCC.v80/ms.EntLib.2006Jan/EnterpriseLibrary/html/04-06-020-Walkthrough%20%20Propagating%20the%20Original%20Exception.htm