Exception Handling Application Block






1.87/5 (9 votes)
This article introduces concept of Exception Handling Application Block and it's usage
Introduction
Exception Handling Application Block. An application block provided by Microsoft to incorporate exception handling in your applications as a standard framework, rather than ad-hoc approach. This article summarizes the basic concept and usage of the same.
Concepts
Process of doing something with an exception when the exception is detected by your code
Process of logging an exception, which might include sending formatted exceptions to the event log or sending an email
Allow you to control exception handling and logging behaviors using external configuration files instead of baking such rules into your code
Using Exception Handling Block
3 things you can do when you detect an exception:
InnerException
property when the new exception is propagated up the call stack.Coding To-dos:
Microsoft.Practices.EnterpriseLibrary.Common.dll Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.dll
app.config
below <configSections>
element under root <configuration>
element:
<section name=“exceptionHandling” type=“Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling” />
<configSections>
element:
<section name=“loggingConfiguration” type=“Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging” />
<exceptionHandling>
element directly under root element <configuration>
, inside which you add all exception policies:
<exceptionHandling> <exceptionPolicies> // Add policy name: see ppt for code sample <exceptionTypes> // Add exception type: see ppt for code sample <exceptionHandlers> // Add handlers: see ppt for code sample </exceptionHandlers> // Close add tag: </exceptionTypes> // Close add tag: </exceptionPolicies> </exceptionHandling>Alternatively, you can add elements easily using Enterprise Library Configuration Tool (part of Visual Studio 2005), as shown:

Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
in your project and write code against classes in the above namespace.
Using ExceptionPolicy classes
Overview
HandleException()
that lets client application interact with Exception Handling Block.HandleException
method uses a factory to create object of type ExceptionPolicyImpl
for supplied policy; this object has a collection of ExceptionPolicyEntry
objects – one object for each exception type specified in configuration file for the named policy; for each exception type, ExceptionPolicyEntry
contains a collection of objects that implement the IExceptionHandler
interface; the collection is ordered and provides the sequence for exception handling block to use when executing the policy; each of these objects have associated configuration information specific to each type of handler.
Example:
Button-click event handler example with simple exception handling (C#)
private void btn_Click(object sender, EventArgs e) { try { throw new Exception(“This is a test exception”); } catch (Exception ex) { bool rethrow = ExceptionPolicy.HandleException(ex, “Global Policy”); if(rethrow) { throw; } } }Button-click event handler example with logging (C#)
// The catch block invokes ExceptionPolicy.HandleException method by passing the exception object (ex) // as well as the policy (“Log Only Policy”) as arguments private void btn_Click(object sender, EventArgs e) { try { throw new Exception(“This is a test exception”); } catch (Exception ex) { bool rethrow = ExceptionPolicy.HandleException(ex, “Log Only Policy”); if(rethrow) { throw; } } }Sample of logging configuration information in
app.config
is shown in the attached PPT.