1

我想使用企业库异常处理块进行异常处理。为了尝试它,我编写了一个简单的应用程序来抛出和处理异常,在使用它时我遇到了以下问题:

当我使用 BCL 异常System.ApplicationException时,抛出的异常会按应有的方式包装:

政策:

<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>

控制台输出:

System.ApplicationException: Dal Wrapper Exception ---> Exceptions.DbPrimitiveHandledException: Db Handled Policed exception...

但是当我尝试使用自己的异常时:

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

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

政策:

<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>

包装不起作用 - 我得到一个 ExceptionHandlingException:

Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingException: Unable to handle exception: 'WrapHandler'.

谁能告诉我我的异常或配置有什么问题?提前致谢

4

1 回答 1

3

问题出在异常类上。它必须再实现一个接受内部异常的构造函数:

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

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

    public DalWrapperException(string message, Exception innerException)
        : base(message, innerException)
    { }
}
于 2013-02-22T17:27:53.530 回答