Click here to Skip to main content
15,881,600 members
Articles / Programming Languages / C#
Article

Exception Handling Application Block

Rate me:
Please Sign up or sign in to vote.
1.65/5 (13 votes)
22 Jan 2007CPOL2 min read 75.4K   1.5K   26   6
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

  • Exception Handling
    Process of doing something with an exception when the exception is detected by your code

  • Exception Logging
    Process of logging an exception, which might include sending formatted exceptions to the event log or sending an email

  • Exception Policies
    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:

  • You can wrap the exception in a new exception to add new context information or detail. The original exception is still available through the InnerException property when the new exception is propagated up the call stack.

  • You can replace the exception with a new exception. Do this when you do not want details of original exception to be propagated across an application boundary.

  • You can log the exception. You can do this in combination with wrapping or replacing the exception, or you can log the original exception and propagate the original or new exception up the call stack.

    Coding To-dos:

  • Add references to:
    Microsoft.Practices.EnterpriseLibrary.Common.dll
    Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll
    Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.dll
  • Add configuration entries to app.config below <configSections> element under root <configuration> element:
    <section
    name="exceptionHandling"
    type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings,
     Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" />
  • For logging, add following sections to <configSections> element:
    <section 
    name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings,
     Microsoft.Practices.EnterpriseLibrary.Logging" />
  • Add <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: </add>
            </exceptionTypes>
         // Close add tag: </add>
      </exceptionPolicies>
    </exceptionHandling>
    Alternatively, you can add elements easily using Enterprise Library Configuration Tool (part of Visual Studio 2005), as shown: Image 1

  • Import namespace of exception handling block Microsoft.Practices.EnterpriseLibrary.ExceptionHandling in your project and write code against classes in the above namespace.

    Using ExceptionPolicy classes

    Overview

  • Class exposes a static method: HandleException() that lets client application interact with Exception Handling Block.

  • You supply policy as an argument; 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.

    References...

  • MSDN on Exception Handling Application Block
  • Blog on EAB
  • License

    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


    Written By
    CEO IDYeah Creations
    India India
    My company: http://www.idyeah.com
    My blog: http://blog.idyeah.com

    Comments and Discussions

     
    GeneralUnhelpful and more cumbersome than original doc Pin
    stodgey1-Dec-08 10:48
    stodgey1-Dec-08 10:48 
    GeneralWorst one to implement for a new bee, Dumb Introduction... Pin
    Manas Mani Tripathi29-Mar-08 4:12
    Manas Mani Tripathi29-Mar-08 4:12 
    GeneralMore research Pin
    Not Active23-Jan-07 5:28
    mentorNot Active23-Jan-07 5:28 
    You need to conduct more research for this article as it is missing large portions, such as creating exception policies and logging, that would make it more useful.

    Essentially the documentation provided with EntLib is a much better resource. This article does not add anything and actually may be more harmful than good since it lacks essential information.


    only two letters away from being an asset

    GeneralRe: More research Pin
    COMKING3-May-07 17:33
    COMKING3-May-07 17:33 
    GeneralNeed additional references Pin
    Not Active23-Jan-07 5:20
    mentorNot Active23-Jan-07 5:20 
    Generalweb app example, and logging in web app Pin
    noemailz23-Jan-07 2:38
    noemailz23-Jan-07 2:38 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.