Enterprise Library, Exception Handling Block: Can't use custom exceptions for wrapping in a WrapHandler

Topics: Exception Handling Application Block
Feb 22, 2013 at 4:17 PM
Edited Feb 22, 2013 at 4:22 PM
I want to use the Enterprise Library Exception Handling Block for exception handling. To try it I wrote a simple app that throws and processes exceptions and while playing with it I encountered the following:

When I use a BCL exception like System.ApplicationException, thrown exceptions are wrapped as they should:

Policy:

<exceptionPolicies>
<add name="DalPolicy">
    <exceptionTypes>
        <add name="DbPrimitiveHandledException" type="Exceptions.DbPrimitiveHandledException, Exceptions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
                <add name="DAL Wrap Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WrapHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                    exceptionMessage="Dal Wrapper Exception" wrapExceptionType="System.ApplicationException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            </exceptionHandlers>
        </add>
    </exceptionTypes>
</add>
...
</exceptionPolicies>
Console output:
System.ApplicationException: Dal Wrapper Exception ---> Exceptions.DbPrimitiveHandledException: Db Handled Policed exception...
But when I try to use my own exception:
public class DalWrapperException : Exception
{
    public DalWrapperException()
    { }

    public DalWrapperException(string message)
        : base(message)
    { }
}
Policy:

<exceptionPolicies>
<add name="DalPolicy">
    <exceptionTypes>
        <add name="DbPrimitiveHandledException" type="Exceptions.DbPrimitiveHandledException, Exceptions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
                <add name="DAL Wrap Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WrapHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                    exceptionMessage="Dal Wrapper Exception" wrapExceptionType="Exceptions.DalWrapperException, Exceptions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
            </exceptionHandlers>
        </add>
    </exceptionTypes>
</add>
...
</exceptionPolicies>
Wrapping doesn't work - I'm getting an ExceptionHandlingException:
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingException: Unable to handle exception: 'WrapHandler'.
Can anybody tell me what's wrong with my exception or configuration? Thanks in advance
Feb 22, 2013 at 4:43 PM
Edited May 1, 2013 at 9:46 PM
The exception used in a Wrap Handler should expose a constructor that accepts a string and an Exception. E.g.:
public class DalWrapperException : Exception
{
    public DalWrapperException()
    { }

    public DalWrapperException(string message)
        : base(message)
    { }

   public DalWrapperException(string message, Exception innerException)
        : base(message, innerException)
    { }
}
~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Feb 22, 2013 at 5:23 PM
Randy,
It works. Thanks a lot!