使用企业库异常处理应用程序模块的优势:
- 它支持整个应用程序体系结构层的异常处理,而不仅限于服务接口的界限。
- 它使得异常处理策略可以在管理层定义和维护,以便决策人员(可能是系统管理员和开发人员)可以定义如何处理异常。他们可以维护和修改控制异常处理的规则集,而无需更改块的应用程序代码。
- 它提供了常用的异常处理功能,例如记录异常信息的功能、通过将原始异常替换为其他异常来隐藏敏感信息的功能,以及通过将原始异常打包到另一个异常中来添加异常的上下文信息的功能。这些功能封装在名为 Exception handlers 的 .NET 类中。
- 它可以合并多个异常处理程序以产生某个异常所需的响应,例如先记录异常信息,再将原始异常替换为其他异常。
- 它使开发人员能够创建自己的异常处理程序。
- 它以一致的方式调用异常处理程序。这意味着,处理程序可以在应用程序之中和之间的多种场合下使用。
下面我们来试试看用EL5.0的异常处理模块到底能做些什么,按需求来做分析是最好的办法,那我们可以模拟的提出下列几个需求,看看用异常处理模块如何灵活的解决它们:
1. 希望能过滤程序中某些异常,即在发生这些异常的时候不会被抛出.要求是配置简单,只用修改一处地方就能控制所有相应的异常.
2. 发生某种异常的时候被自动替换成另外一个异常
3. 发生某种异常的时候被自动包装到另外一个异常中
4. 发生某种异常的时候被自动记录在指定的日志记录策略中,可以是记录到数据库或者文件中.
下面介绍如何使用Microsoft Enterprise Library 5.0中的异常处理程序模块来处理上面的问题:
1. 运行EntLibConfig.exe, 选择Blocks菜单 ,单击 Add Exception Handling Settings .
2. 为了模拟第一个问题,我们要先删除原有的All Exceptions,因为它表示所有的异常均截取,我们删除了它,再在Policy面板上右键—Add Exception Type,在弹出的异常类型选择窗口中,我们选择一个异常System.FormatException:
3. 点击 File菜单,单击 Save,保存为一个App.config文件,可以先保存到桌面,之后要用到它.
4. 创建一个新的控制台应用程序,将App.config添加到程序内,并加入需要的Dll文件,并添加需要的引用:
添加引用:
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;

using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Collections.Specialized;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;
namespace test
{
class Program
{
static void Main( string[] args)
{
bool haveException = false;
ExceptionManager em = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
try
{
// FormatException异常捕获设置为None,将不会被抛出
em.Process(NoThrowException, " Policy ");
}
catch (Exception)
{
haveException = true;
Console.WriteLine( " 捕获到异常! ");
}
if (haveException == false)
{
Console.WriteLine( " 未捕获到任何异常! ");
}
}
private static void NoThrowException()
{
int i = Int32.Parse( " A ");
Console.WriteLine( " 发生异常,不执行该指令 ");
}
}
}
运行结果:
到此为止,我们已经解决了第一个问题,当你的程序想只过滤System.FormatException异常的时候,可以用上面的方法实现,如果想过滤其他的异常,只用在EL5中添加即可,无需更改程序中的任何代码.
5. 下面我们来解决第二个问题,让我们回到EL5.0,在Policy面板上右键—Add Exception Type,在弹出的异常类型选择窗口中,我们选择一个异常System.IO.FileNotFoundException,再在FileNotFoundException面板上右键—Add Handlers—Add Replace Handler,在建立好的Replace Handler面板中点击Replace Exception Type右边的…按钮,选择要替换成的异常类型,我们选择:System.TimeputException:
6. Save一下,更新App.config文件,修改源程序如下:

using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Collections.Specialized;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;
namespace test
{
class Program
{
static void Main( string[] args)
{
// bool haveException = false;
ExceptionManager em = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
// try
// {
// // FormatException异常捕获设置为None,将不会被抛出
// em.Process(NoThrowException, "Policy");
// }
// catch (Exception)
// {
// haveException = true;
// Console.WriteLine("捕获到异常!");
// }
// if (haveException == false)
// {
// Console.WriteLine("未捕获到任何异常!");
// }
// FileNotFoundException异常捕获被设置为ThrowNewException,将被替换为TimeoutException异常
try
{
em.Process(ReplaceHandler, " Policy ");
}
catch (TimeoutException e)
{
Console.WriteLine( " 捕获到TimeoutException异常!异常信息: "+ e.Message);
}
}
private static void ReplaceHandler()
{
File.Open( " test.txt ", FileMode.Open);
Console.WriteLine( " 发生异常,不执行该指令 ");
}
private static void NoThrowException()
{
int i = Int32.Parse( " A ");
Console.WriteLine( " 发生异常,不执行该指令 ");
}
}
}
7. 运行结果:

到此为止我们便解决了第二个问题,是不是很简单呀,这就是EL的便利之处~
8. 接着处理第三个问题,回到EL,在Policy面板上右键—Add Exception Type,在弹出的异常类型选择窗口中,我们选择一个异常System. NullReferenceException,再在NullReferenceException面板上右键—Add Handlers—Add Wrap Handler,在建立好的Wrap Handler面板中点击Wrap Exception Type右边的…按钮,选择要替换成的异常类型,我们选择: System.ApplicationException:
9. Save一下,更新App.config文件,修改源程序如下:

using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Collections.Specialized;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;
namespace test
{
class Program
{
static void Main( string[] args)
{
// bool haveException = false;
ExceptionManager em = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
// try
// {
// // FormatException异常捕获设置为None,将不会被抛出
// em.Process(NoThrowException, "Policy");
// }
// catch (Exception)
// {
// haveException = true;
// Console.WriteLine("捕获到异常!");
// }
// if (haveException == false)
// {
// Console.WriteLine("未捕获到任何异常!");
// }
// Console.WriteLine("-----------------------------------------------");
/// /FileNotFoundException异常捕获被设置为ThrowNewException,将被替换为TimeoutException异常
// try
// {
// em.Process(ReplaceHandler, "Policy");
// }
// catch (TimeoutException e)
// {
// Console.WriteLine("捕获到TimeoutException异常!异常信息:" + e.Message);
// }
// Console.WriteLine("-----------------------------------------------");
/// /NullReferenceException异常捕获被设置为ThrowNewException,将被包装为ApplicationException异常
try
{
em.Process(WrapHandler, " Policy ");
}
catch (ApplicationException e)
{
Console.WriteLine( " 捕获到ApplicationException异常,其被包装的异常为{0}! ", e.InnerException.GetType().ToString());
Console.WriteLine( " 异常信息:{0} ", e.Message);
}
}
private static void WrapHandler()
{
Hashtable ht = new Hashtable();
ht[ " test "].ToString();
Console.WriteLine( " 发生异常,不执行该指令 ");
}
private static void ReplaceHandler()
{
File.Open( " test.txt ", FileMode.Open);
Console.WriteLine( " 发生异常,不执行该指令 ");
}
private static void NoThrowException()
{
int i = Int32.Parse( " A ");
Console.WriteLine( " 发生异常,不执行该指令 ");
}
}
}
10. 运行结果:
到此为止,我们又解决了第三个问题.
11. 接着处理第四个问题,回到EL,在Policy面板上右键—Add Exception Type,在弹出的异常类型选择窗口中,我们选择一个异常System. NullReferenceException,再在NullReferenceException面板上右键—Add Handlers—Add Logging Exception Handler,将日志策略设置为文件记录方式,详细步骤在此不多讲,大家可以看看我之前写的日志处理模块教程:
12. Save一下,更新App.config文件,修改源程序如下:

using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Collections.Specialized;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;
namespace test
{
class Program
{
static void Main( string[] args)
{
// bool haveException = false;
ExceptionManager em = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
// try
// {
// // FormatException异常捕获设置为None,将不会被抛出
// em.Process(NoThrowException, "Policy");
// }
// catch (Exception)
// {
// haveException = true;
// Console.WriteLine("捕获到异常!");
// }
// if (haveException == false)
// {
// Console.WriteLine("未捕获到任何异常!");
// }
// Console.WriteLine("-----------------------------------------------");
/// /FileNotFoundException异常捕获被设置为ThrowNewException,将被替换为TimeoutException异常
// try
// {
// em.Process(ReplaceHandler, "Policy");
// }
// catch (TimeoutException e)
// {
// Console.WriteLine("捕获到TimeoutException异常!异常信息:" + e.Message);
// }
// Console.WriteLine("-----------------------------------------------");
/// /NullReferenceException异常捕获被设置为ThrowNewException,将被包装为ApplicationException异常
// try
// {
// em.Process(WrapHandler, "Policy");
// }
// catch (ApplicationException e)
// {
// Console.WriteLine("捕获到ApplicationException异常,其被包装的异常为{0}!", e.InnerException.GetType().ToString());
// Console.WriteLine("异常信息:{0}", e.Message);
// }
// Console.WriteLine("-----------------------------------------------");
// 捕获ArgumentOutOfRangeException异常,并写入日志
try
{
em.Process(NotifyRethrow, " Policy ");
}
catch (ArgumentOutOfRangeException)
{
Console.WriteLine( " 捕获到ArgumentOutOfRangeException异常,并写入日志! ");
}
}
private static void NotifyRethrow()
{
List< string> list = new List< string>();
string str = list[ 1];
Console.WriteLine( " 发生异常,不执行该指令 ");
}
private static void WrapHandler()
{
Hashtable ht = new Hashtable();
ht[ " test "].ToString();
Console.WriteLine( " 发生异常,不执行该指令 ");
}
private static void ReplaceHandler()
{
File.Open( " test.txt ", FileMode.Open);
Console.WriteLine( " 发生异常,不执行该指令 ");
}
private static void NoThrowException()
{
int i = Int32.Parse( " A ");
Console.WriteLine( " 发生异常,不执行该指令 ");
}
}
}
13. 运行结果:
打开工程目录下的rolling.log文件: