企业库,异常处理块:Can';不要在WrapHandler中使用自定义异常进行包装

本文关键字:WrapHandler 自定义异常 包装 异常处理 Can 企业库 | 更新日期: 2023-09-27 18:00:30

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

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

政策:

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

控制台输出:

系统。ApplicationException:Dal包装器异常--->例外情况。DbPrimitiveHandledException:Db已处理的Policed异常。。。

但当我尝试使用我自己的例外:

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>

包装不起作用-我收到一个异常HandlingException:

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

有人能告诉我我的异常或配置出了什么问题吗?提前感谢

企业库,异常处理块:Can';不要在WrapHandler中使用自定义异常进行包装

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

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