添加项目

1.打开VS2005 ,新建一个名为ExceptionStudy的解决方数,并添加一个名为QuickStartExcutableWindow应用程序项目。

 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类新文件,并在其中添加两个办法来实现引发异常与处理异常情况。

 

EnterpriseLibrary2(异常的传播与处理)using System;
EnterpriseLibrary2(异常的传播与处理)
using System.Collections.Generic;
EnterpriseLibrary2(异常的传播与处理)
using System.Text;
EnterpriseLibrary2(异常的传播与处理)
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
EnterpriseLibrary2(异常的传播与处理)
EnterpriseLibrary2(异常的传播与处理)
namespace BusinessLayerBLL

 

6.QuickStartExcutable项目中添加一个AppMessageExceptionHandler的新文件,来实现自定义的异常处理,其要必须符合两个条件:

 A.实现继承IExceptionHandler接品口

 B.类声明 [ConfigurationElementType(typeof(CustomHandlerData))]属性

EnterpriseLibrary2(异常的传播与处理)using System.IO;
EnterpriseLibrary2(异常的传播与处理)
using System;
EnterpriseLibrary2(异常的传播与处理)
using System.Text;
EnterpriseLibrary2(异常的传播与处理)
using System.Windows.Forms;
EnterpriseLibrary2(异常的传播与处理)
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
EnterpriseLibrary2(异常的传播与处理)
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
EnterpriseLibrary2(异常的传播与处理)
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;
EnterpriseLibrary2(异常的传播与处理)
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
EnterpriseLibrary2(异常的传播与处理)
using System.Collections.Specialized;
EnterpriseLibrary2(异常的传播与处理)
EnterpriseLibrary2(异常的传播与处理)
namespace QuickStactExcutable




7.Form1里添加一个Button名为b_CatchException,添加Button的事件处理的办法如下:

        }

 

   创建异常处理策略配置文件:

8.打开Enterprise Library Configuration ,右键New Applicaton,设置ConfigurationFile,把它的路径选择在ExceptionStudy存放的目录下,并命名为App

9.右键->New->Exception Handling Application Bock

10.右键->New->Exception Policy,NamePropagate Policy

8.右键->New->Exception Type ,类型为Exception,PostHandlingActionNotifyRethrow.

11.重复7,Name为:Global Policy,

12,重复8, PostHandlingActionNone;

13.右键->New->Custom Handler ,设置type ->Load Assembly->浏览到   QuickStartExcutable项目的文件夹,bin\Debug下选择QuickStartExcutable.exe,点击OK即可。

14.分别在两个项目里,添加现有项,文件类型为所有文件类型,然后找到上面创建的配置文件,添加就行了。

 15.到这里整个解决方案就完成了,运行程序就可以啦。点击Button后就可以显示异常的原始信息。
    EnterpriseLibrary2(异常的传播与处理)

参考文档ms-help://MS.VSCC.v80/MS.VSIPCC.v80/ms.EntLib.2006Jan/EnterpriseLibrary/html/04-06-020-Walkthrough%20%20Propagating%20the%20Original%20Exception.htm

相关文章: