Exception handling (Exception handling) is one of the most basic operations of all systems, other such as log (Logging), Audit (Auditing), Cache (Caching), transaction processing (Transaction), etc.
Today, to introduce exception handling into our example of calculator in the "MVP V and P interactions", we simply implement AOP. Instance runs
Well, let's start with a brief introduction of the Enterprise Library Ehab(Exception handling application Block) Provides a policy-based approach to exception handling. Specific can be referred to here .
How to configure
The specific configuration is as follows:
<Configuration> <configsections> < Sectionname= "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 <Addname= "Uiexceptionpolicy">4 <Exceptiontypes>5 <Addname= "All Exceptions"type= "System.Exception, mscorlib, version=4.0.0.0, Culture=neutral, publickeytoken=b77a5c561934e089"6 posthandlingaction= "None">7 <exceptionhandlers>8 <Addtype= "Handwe.Demo.UnityInMVP.MessageBoxHandler, Handwe.Demo.UnityInMVP"9 name= "Custome Handler" />Ten </exceptionhandlers> One </Add> A </Exceptiontypes> - </Add> - </exceptionpolicies> the </exceptionhandling>
These can be configured by means of configuration tools; Now let's talk about specific things:
< exceptionpolicies > < name= "Uiexceptionpolicy">
Add an exception policy named Uiexceptionpolicy;
1 < name= "All Exceptions" type= "System.Exception, mscorlib, version=4.0.0.0, Culture=neutral, publickeytoken=b77a5c561934e089 "2 posthandlingaction=" None " >
Configure the type of exception to be handled, here are all exceptions; posthandlingaction= "None" is no follow-up treatment;
1 < exceptionhandlers > 2 < type= "Handwe.Demo.UnityInMVP.MessageBoxHandler, Handwe.Demo.UnityInMVP"3 name= "Custome Handler"/>
Exceptionhandlers added is a custom handler, called Messageboxhandler, is simply a pop-up message box;
implementation of the Code
Using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
Using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
Using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;
- Exception handler Messageboxhandler
1 namespaceHandwe.Demo.UnityInMVP2 {3[ConfigurationElementType (typeof(Customhandlerdata))]4 Public classMessageboxhandler:iexceptionhandler5 {6 PublicMessageboxhandler (NameValueCollection igonre)7 {8 9 }Ten PublicException handleexception (Exception Exception, Guid Handlinginstanceid) One { AMessageBox.Show (Exception. Message,"Application Error", MessageBoxButtons.OK, messageboxicon.error); - returnexception; - } the } -}
Here is very simple and does not use the corresponding parameter configuration;
Try { this. Oncalculating (OP1, OP2); } Catch (Exception ex) { if"uiexceptionpolicy")) { throw; } }
Modify and apply the above code, and specify exception handling policy;
Summary
With EntLib's Ehab, we can focus on specific business logic, and non-business processes such as exceptions can be implemented through a later configuration.
Exception Handling introduced MVP