Exception Handling引入MVP

异常处理(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>
 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>

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

<exceptionPolicies>
      <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 }

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

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

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

小结

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

时间: 2024-10-13 20:46:45

Exception Handling引入MVP的相关文章

[转载]A Crash Course on the Depths of Win32 Structured Exception Handling

转自:[已完工][经典文章翻译]A Crash Course on the Depths of Win32 Structured Exception Handling 原文题目: <<A Crash Course on the Depths of Win32™ Structured Exception Handling>> 原文地址: http://www.microsoft.com/msj/0197/Exception/Exception.aspx 原作者: Matt Pietr

OAF_OAF Exception Handling异常处理(概念)

2014-06-12 BaoXinjian 一.摘要 Oracle Application Framework将异常分为三类 异常类型type 1. 常规异常General Exception Class:oracle.apps.fnd.framework.OAException Function: 提供了在运行时刻同时显示多种类型异常的手段,结合EBS的Message,可显示有用的信息 2. 验证异常Validation Exception Class:oracle.apps.fnd.fram

/EH (Exception Handling Model)

Specifies the kind of exception handling used by the compiler, when to optimize away exception checks, and whether to destroy C++ objects that go out of scope because of an exception. If /EH is not specified, the compiler catches both asynchronous st

Exception handling

https://en.wikipedia.org/wiki/Exception_handling In general, an exception breaks the normal flow of execution and executes a pre-registered exception handler. The details of how this is done depends on whether it is a hardware or software exception a

[转]java-Three Rules for Effective Exception Handling

主要讲java中处理异常的三个原则: 原文链接:https://today.java.net/pub/a/today/2003/12/04/exceptions.html Exceptions in Java provide a consistent mechanism for identifying and responding to error conditions. Effective exception handling   will make your programs more ro

Declarative Exception Handling in Struts

Declarative Exception Handling is the procedure of handling Exceptions declaratively through the help of xml files. It is not needed to include exception-handling code in the java code of the application. The way of handling the exception would be sp

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

原文:http://www.asp.net/web-api/overview/error-handling/exception-handling This article describes error and exception handling in ASP.NET Web API. HttpResponseException Exception Filters Registering Exception Filters HttpError HttpResponseException Wha

CoreCLR on Mac:体验managed exception handling

C#测试代码: using System; class Program { static void A() { try { Console.WriteLine("Throwing an exception"); throw new Exception("Did you catch it?"); } finally { Console.WriteLine("A.finally()"); } } static void B() { try { C()

SEH(structured exception handling)基础篇---终止处理程序

前序:最近看SEH看的头晕脑胀/(ㄒoㄒ)/~~,SEH最开始是Windows提供的异常处理机制,只是一个简单的框架,而我们现在使用的SEH都是编译器已经在系统提供的最基础的框架上做了修改的增强版(原始版比较原始,牵扯到大量Windows基础知识,并且需要反汇编看汇编代码来理解....本人现在功力较浅,需要慢慢消化,先看一下增强版,通过C和C++来接触一下. SEH包含两部分功能:终止处理(termination handling)和异常处理(exception handling).在这先讲一下