Exception Handling Application Block (5)详细解
来源:互联网 发布:神机妙算2016软件介绍 编辑:程序博客网 时间:2024/05/17 23:49
添加引用:
获取Facade类
ExceptionManager ExManager = EnterpriseLibraryContainer.Current.GetInstance
EnterpriseLibraryContainer位于Microsoft.Practices.EnterpriseLibrary.Common.Configuration;需要using下.
错误处理样板代码:
//==================
try{
.....你的操作,可能会抛出错误
}catch(Exception ex){
if (ExManager.HandleException(ex, "错误处理策略名称")) throw;
}
//===============
"错误处理策略名称",指你在错误处理模块配置指定的名称。
也可以使用 return ExceptionManager.Process(()=>MyMethod(18,"sss",...)) 进行调用
下面是Process的实现代码,跟上面的样板代码一样
///
/// Executes the supplied delegate, and handles
/// any thrown exception according to the rules configured for.
///
///Type of return value from .
/// The delegate to execute.
/// The value to return if an exception is thrown and the
/// exception policy swallows it instead of rethrowing.
/// The name of the policy to handle.
///If no exception occurs, returns the result from executing . If
/// an exception occurs and the policy does not re-throw, returns.
public override TResult Process<TResult>(Func<TResult> action, TResult defaultResult, string policyName)
{
if(action == null) throw new ArgumentNullException("action");
if(policyName == null) throw new ArgumentNullException("policyName");
try
{
return action();
}
catch (Exception e)
{
if(HandleException(e, policyName))
{
throw;
}
}
return defaultResult;
}
如果你电脑上有多个Ent Lib相关程序集(Release,Src,Hands On labs)请注意程序集的引用,默认情况下强类型程序集(PublicKeyToken!=null)是不允许引用未签名的程序集的,下面是可能的错误提示。
//===============引用错误===============
创建 exceptionHandling 的配置节处理程序时出错: 未能加载文件或程序集“Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)
//=================
为避免这个问题请在程序中引用跟配置工具(EntLibConfig.exe)同目录的程序集
参考下图可知道,一个Policy,会捕获一组错误,而每个错误,多允许配置多个处理者(Handler)
这个处理者的动作包括替换,包装,记录等处理。
每个错误类型可以设置Post Handling action, 分别取None,NotifyRethrow,thorwNewExcetpion等三个枚举
指定的错误被捕获,HandleException会根据配置逐一执行Handle,接着就根据Post Handling action设置进行相应处理
这个设置需要配合上面的样板代码来实现功能,列举如下:
None:HandleException返回false,不会执行throw;故错误将就此“消失”
NotifyRethrow:HandleException返回true,throw将被执行,原始错误被抛出。
throwNewExcetpion:HandleException返回true,throw将被执行,但是错误类型被替换成Wrap Handler或ReplaceHandler指定的类型,错误抛出点(Stack trace)将会出现在Handle内部(企业库),当然Wrap Handler会将原始错误封装在innerException中。
注意throw;跟throw ex;是不同的 throw ex将会将原始错误发生点改成当前throw ex的位置。
- Exception Handling Application Block (5)详细解
- Logging & Exception Handling Application Block
- Enterprise Library: Exception Handling Application Block类设计分析
- Enterprise Library Exception Handling Application Block 学习笔记 - 1
- Enterprise Library Exception Handling Application Block 学习笔记 - 2
- Enterprise Library Exception Handling Application Block 学习笔记 - 3
- Microsoft Enterprise Library 5.0 系列(七) Exception Handling Application Block
- Microsoft Enterprise Library 5.0 系列(七) Exception Handling Application Block
- Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block
- Exception Management Application Block
- finally block and Exception handling -- Java
- Centralised Exception Handling in C# Windows Application
- Manage Exception Handling in Spring Application
- Preventing application crash by using exception handling mechanism
- Exception handling
- Exception Handling
- Exception Handling
- Exception Handling
- ASP.NET 异常处理
- 多功能抽奖系统,为网吧创收更多利润
- 0001+1怎么才能等于0002啊
- direct3d D3DXCreateTextureFromResource 加载png
- 最短路径---Dijkstra备忘
- Exception Handling Application Block (5)详细解
- 安装 VS 2005 SP 1 出现 Internal Error 2755 1612 解决办法
- Enterprise Library Policy Injection Application Block(5.0) 函数调用链整理
- cnblogs.com精华收藏帖
- Asp.net的一些编码问题
- Repeater控件备忘
- 计算ScriptResource.axd的d参数
- 关于控件Visible属性的说明
- 多级分类,按用户配置可选-使用三组ListBox(参考taobao发布)