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

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

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

如何配置

具体的配置如下:

  1. <configuration>
  2. <configSections>
  3. <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" />
  4. </configSections>
  1. <exceptionHandling>
  2. <exceptionPolicies>
  3. <add name="UIExceptionPolicy">
  4. <exceptionTypes>
  5. <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
  6. postHandlingAction="None">
  7. <exceptionHandlers>
  8. <add type="Handwe.Demo.UnityInMVP.MessageBoxHandler, Handwe.Demo.UnityInMVP"
  9. name="Custome Handler" />
  10. </exceptionHandlers>
  11. </add>
  12. </exceptionTypes>
  13. </add>
  14. </exceptionPolicies>
  15. </exceptionHandling>

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

  1. <exceptionPolicies>
  2. <add name="UIExceptionPolicy">

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

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

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

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

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

代码的实现

  • 引用程序集

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

  • 异常处理程序MessageBoxHandler
  1. namespace Handwe.Demo.UnityInMVP
  2. {
  3. [ConfigurationElementType(typeof(CustomHandlerData))]
  4. public class MessageBoxHandler : IExceptionHandler
  5. {
  6. public MessageBoxHandler(NameValueCollection igonre)
  7. {
  8.  
  9. }
  10. public Exception HandleException(Exception exception, Guid handlingInstanceId)
  11. {
  12. MessageBox.Show(exception.Message, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  13. return exception;
  14. }
  15. }
  16. }

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

  1. try
  2. {
  3. this.OnCalculating(op1, op2);
  4. }
  5. catch (Exception ex)
  6. {
  7. if (ExceptionPolicy.HandleException(ex, "UIExceptionPolicy"))
  8. {
  9. throw;
  10. }
  11. }

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

小结

通过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. Webstorm常用的快捷键

    WS的常用操作: 常用快捷键(Keymap/Eclipse): 复制当前行: Ctrl+Alt+↓ 向上/下移动当前行: Alt+↑/↓ 删除当前行: Ctrl+D 注释/取消当前行: Ctrl+/ ...

  2. 深入浅出node(3) 异步I/O

    这篇主要整理深入浅出Node.js第三章 异步I/O 一) 异步I/O的原因 二)异步I/O实现现状 2.1 异步I/O与非阻塞I/O 2.2 轮询 2.3 理想的非阻塞异步I/O 2.4 现实的异步 ...

  3. UIPickerView的使用(一)

    简介:UIPickerView是一个选择器控件,它比UIDatePicker更加通用,它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活.UIPick ...

  4. dojo.require()的相关理解

    Dojo 提供了一个非常强大的javascript控件库. 在使用dojo之前,用户基本上不需要具备任何基础知识. 你可以用script远程链接到dojo(dojo.js), 也可以把dojo.js下 ...

  5. SharePoint 2013 使用JavaScript对象模型配置智能提示

    前言 默认在VS2012/2013中编写SharePoint JavaScript 客户端对象模型,都没有智能感知的功能,用起来非常麻烦:其实,我们可以手动配置一下,让JavaScript可以进行智能 ...

  6. Android Do not keep activities选项分析

    Android Do not keep activities选项分析 Developer Options里面有一项: Do not keep activities -> 不保留Activitie ...

  7. TabLayout和ViewPager简单实现页卡的滑动

    首先需要在当前的module中的build Gradle的 dependencies中加入以下句子 compile 'com.android.support:design:23.0.1' 因为我们用到 ...

  8. 基于Ruby的watir-webdriver自动化测试方案与实施(四)

    接着基于Ruby的watir-webdriver自动化测试方案与实施(三) http://www.cnblogs.com/Javame/p/4159468.html 继续 ... ...   首先回忆 ...

  9. 1、软件工程师要阅读的书籍 - IT软件人员书籍系列文章

    软件工程师要阅读的书籍估计是项目组内最多的.软件工程师处于项目组中最基础的人员储备阶层,与项目的关系最密切.当然,现在是大数据时代,我们无法全部看完所有相关的书籍,只能够先学习工作需要的知识,然后在项 ...

  10. Java暗箱操作之自动装箱与拆箱

    我以前在写Android项目的时候,估计写得最多最熟练的几句话就是: List<Integer> list = new ArrayList<Integer>(); list.a ...