Customized Exception Handling

In one of our project, we were thinking of replacing the existing our own created Exception handling, with Enterprise library for exception handling. We are able to integrate the Exception handling name space without any problem. And are using, their various Exception handling policies like Replace Policy, Wrap Policy, Propagate Policy etc.

But, our requirement is something that

  1. All the business layers will throw the error number
  2. All the layers, should throw the error, as is, till the exist / entry point of your functionality
  3. The error can be the business exception with error number (known error) or some system exception (un-handled errors)
  4. The entry point component / interface should handle the error. Means, the error number should be replaced with the actual message and any unhanded errors should be wrapped with the some sweat message.
  5. Error Code 10001 : Message : No more results found in your search
  6. DBexcetpion : A Unexpected error happened in the server, please try after some time

We decided to use the exception handling library to solve all these problems.

  1. Replace Policy : Should be used for business exceptions
  2. Wrap Policy : For any un-handled errors
  3. Propagate Policy : In all classes / functions other than the entry/exit points

Existing replace Policy functionality

  1. Currently the replace policy is configured as below, a replace message to each of the type of the exception. Here the configuration is to handle "System.Security.SecurityException"

    <add name="Replace Policy">
                <exceptionTypes>
                <
    add name="SecurityException" type="System.Security.SecurityException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="ThrowNewException">
                <exceptionHandlers>
                <
    add name="Replace Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" exceptionMessage="Replaced Exception: User is not authorized to peform the requested action." replaceExceptionType="System.ApplicationException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
               </exceptionHandlers>
          </
    add>
        </
    exceptionTypes>
    </
    add>

  2.  

  3. To call this you need to raise a "System.Security.SecurityException" and the error message will be replaced by this message.

You can add more exception types like database under the "Replace Policy", with different messages.

Wrap Policy Functionality

<add name="Wrap Policy">
 <exceptionTypes>
    <
add name="NonHandledErrors" type="System.Exception" postHandlingAction="ThrowNewException">
       <exceptionHandlers>
         <
add name="Wrap Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WrapHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" exceptionMessage="A recoverable error occurred while processing your request, please try after some time" wrapExceptionType="System.Exception" />
         </exceptionHandlers>
        </
add>
     </
exceptionTypes>
  </
add>

Our requirement

In our case the exception type will be "BusinessException" (Custom exception) in case of any business related exceptions and and system.Exception for all other un-handled exception.

Here we are defined two error codes and its messages, 100001, 100002.

   <
add name="Replace Policy">
      <exceptionTypes>
         <
add name="BusinessLayerException" type="System.Exception" postHandlingAction="ThrowNewException">
           <exceptionHandlers>

              <add name="100001" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" exceptionMessage="No Flight options found for this query" replaceExceptionType="System.ApplicationException" />

              <add name="100002" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" exceptionMessage="Not able to book your requrierment" replaceExceptionType="System.ApplicationException" />
            </exceptionHandlers>
         </
add>
     </
exceptionTypes>
 </
add>

We found that, the Enterprise Library exception replace handler required some modification and will be able to cope this requirements. It's good to see that Microsoft shared the source code of the exception handling, and thus, modified some classes to meet our requirements.

Code for the entry/exist point in your code

You need to handle the error here

catch (Exception ex)
            {
                bool rethrow = false;
                if (ex.GetType().FullName.Equals("ExceptionHandling.BusinessException"))
                {
                    rethrow = ExceptionPolicy.HandleException(ex, EnumExceptionPolicyTypes.Type.Replace_Policy);
                }
                else
                {

                    rethrow = ExceptionPolicy.HandleException(ex, EnumExceptionPolicyTypes.Type.Wrap_Policy);
                }
                if (rethrow)
                {
                    throw;
                }
            }

Code for all other codes / functions

  catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, EnumExceptionPolicyTypes.Type.Propagate_Policy);
                if (rethrow)
                {
                    throw;
                }      

            }

Way to rise raises your own exception

throw new BusinessException("100001");

Customer business exception Class

public class BusinessException : System.ApplicationException
    {
        public BusinessException() : base() { }
        public BusinessException(string message) : base(message) { }
        public BusinessException(string message, System.Exception inner) : base(message, inner) {
}

    }

You can download the customize EnterpriseLibrary.ExceptionHandling here, this code is same microsoft code, with changes in clases.


Similar Articles