异常处理(Exception Handling)是所有系统的最基本的基础操作之一,其它的比如日志(Logging)、审核(Auditing)、缓存(Caching)、事务处理(Transaction)等;

今天,来把异常处理引入到我们在《MVP之V和P的交互》中Calculator的实例中,简单的实现AOP。实例运行如图:

那么,开始我们开简单的介绍下Enterprise Library EHAB(Exception Handling Application Block)提供了一种基于策略(Policy)的异常处理方式。具体的可以参考这里

如何配置

具体的配置如下:

<configuration>
<configSections>
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
   <exceptionHandling>
<exceptionPolicies>
<add name="UIExceptionPolicy">
<exceptionTypes>
<add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="None">
<exceptionHandlers>
<add type="Handwe.Demo.UnityInMVP.MessageBoxHandler, Handwe.Demo.UnityInMVP"
name="Custome Handler" />
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>

这些是可以通过配置工具来配置的;现在我们来说说具体的内容:

<exceptionPolicies>
<add name="UIExceptionPolicy">

添加一个名为UIExceptionPolicy的异常策略;

 <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="None">

配置要处理的异常类型,这里是所有异常;postHandlingAction="None"是无后续处理;

 <exceptionHandlers>
<add type="Handwe.Demo.UnityInMVP.MessageBoxHandler, Handwe.Demo.UnityInMVP"
name="Custome Handler" />

exceptionHandlers添加的是一个我们自定义的处理程序,名为MessageBoxHandler,就是简单的一个弹出式消息框;

代码的实现

  • 引用程序集

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;

  • 异常处理程序MessageBoxHandler
 namespace Handwe.Demo.UnityInMVP
{
[ConfigurationElementType(typeof(CustomHandlerData))]
public class MessageBoxHandler : IExceptionHandler
{
public MessageBoxHandler(NameValueCollection igonre)
{ }
public Exception HandleException(Exception exception, Guid handlingInstanceId)
{
MessageBox.Show(exception.Message, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return exception;
}
}
}

这里很简单也没有用到相应的参数配置;

try
{
this.OnCalculating(op1, op2);
}
catch (Exception ex)
{
if (ExceptionPolicy.HandleException(ex, "UIExceptionPolicy"))
{
throw;
}
}

修改并应用以上代码;指定异常处理策略;

小结

通过Entlib的EHAB,我们可以专注于具体的业务逻辑上,类似异常之类的非业务处理可以通过后期的配置来实现。

Exception Handling引入MVP的更多相关文章

  1. Unity、Exception Handling引入MVP

    什么是MVP?在“MVP初探”里就有讲过了,就是一种UI的架构模式. 简单的描述一下Unity和Exception Handling Application Block: Unity是一个轻量级的可扩 ...

  2. Exception Handling Considered Harmful

    异常处理被认为存在缺陷 Do, or do not. There is no try. - Yoda, The Empire Strikes Back (George Lucas) by Jason ...

  3. Exception Handling in ASP.NET Web API webapi异常处理

    原文:http://www.asp.net/web-api/overview/error-handling/exception-handling This article describes erro ...

  4. CoreCLR on Mac:体验managed exception handling

    C#测试代码: using System; class Program { static void A() { try { Console.WriteLine("Throwing an ex ...

  5. Exception Handling Statements (C# Reference)

    Exception Handling Statements (C# Reference) C# provides built-in support for handling anomalous sit ...

  6. Exception Handling in ASP.NET Web API

    public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErr ...

  7. [转]java-Three Rules for Effective Exception Handling

    主要讲java中处理异常的三个原则: 原文链接:https://today.java.net/pub/a/today/2003/12/04/exceptions.html Exceptions in ...

  8. How a C++ compiler implements exception handling

    Introduction One of the revolutionary features of C++ over traditional languages is its support for ...

  9. 黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block 使用企业库异常处理应用程序模块的 ...

随机推荐

  1. jquery根据name属性查找

    $("div[id]") 选择所有含有id属性的div元素 $("input[name='keleyicom']") 选择所有的name属性等于'keleyic ...

  2. RequireJS 模块的定义与加载

    模块不同于传统的脚本文件,它良好地定义了一个作用域来避免全局名称空间污染.它可以显式地列出其依赖关系,并以函数(定义此模块的那个函数)参数的形式将这些依赖进行注入,而无需引用全局变量.RequireJ ...

  3. iOS 开源项目

    在 Github 上 Star 太多了,有时候很难找到自己想要的开源库,所以在此记录下来.便于自己开发使用,也顺便分享给大家. 动画 awesome-ios-animation收集了iOS平台下比较主 ...

  4. arcgis10.2.2地图服务切图具体步骤

    1.未发布的参照http://www.cnblogs.com/oolili/p/4752114.html 2.在发布地图服务的基础上,打开Arccatalog,找到发布的地图服务选项,如下图: 右键地 ...

  5. AJAX请求详解 同步异步 GET和POST

    AJAX请求详解 同步异步 GET和POST 上一篇博文(http://www.cnblogs.com/mengdd/p/4191941.html)介绍了AJAX的概念和基本使用,附有一个小例子,下面 ...

  6. Java Web中乱码问题

    response.setContentType("text/html;charset=UTF-8"); 用于定义网络文件的类型和网页的编码,决定浏览器将以什么形式.什么编码读取这个 ...

  7. Android 手机卫士--设置密码对话框

    本文实现初次设置密码验证过程,首先实现如下效果 本文地址:http://www.cnblogs.com/wuyudong/p/5939823.html,转载请注明出处. 布局如下: <?xml ...

  8. 中断ORACLE数据库关闭进程导致错误案例

    昨晚下班的时候,我准备关闭本机的虚拟机上的ORACLE数据库后准备下班,但是由于我SecureCRT开了多个窗口,结果一不小心,疏忽之下在一个生产服务器上执行了shutdown immediate命令 ...

  9. [AlwaysOn Availability Groups]DMV和系统目录视图

    DMV和系统目录视图 这里主要介绍AlwaysON的动态管理视图,可以用来监控和排查你的AG. 在AlwaysOn Dashboard,你可以简单的配置的GUI显示很多可用副本的DMV和可用数据库通过 ...

  10. MySQL Performance-Schema(一) 配置篇

    performance-schema最早在MYSQL 5.5中出现,而现在5.6,5.7中performance-Schema又添加了更多的监控项,统计信息也更丰富,越来越有ORACLE-AWR统计信 ...