serge.karalenka
serge.karalenka

Reputation: 990

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

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

Upvotes: 1

Views: 2360

Answers (1)

serge.karalenka
serge.karalenka

Reputation: 990

The problem was with the exception class. It must implement one more constructor that accepts an inner exception:

public class DalWrapperException : Exception
{
    public DalWrapperException()
    { }

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

    public DalWrapperException(string message, Exception innerException)
        : base(message, innerException)
    { }
}

Upvotes: 3

Related Questions