2013-02-22 74 views
1

我想使用企业库异常处理块进行异常处理。 要尝试它,我写了一个简单的应用程序,抛出和处理异常,而与它打我遇到以下:企业库,异常处理块:不能使用自定义异常来封装WrapHandler

当我使用一个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:达尔包装异常---> Exceptions.DbPrimitiveHandledException:DB处理好警力例外...

但是当我试图用我自己的异常:

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

有人可以告诉我我的异常或配置有什么问题吗? 在此先感谢

回答

3

问题是与异常类。它必须实现一个接受内部异常的构造函数:

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

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

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

Tuzo,谢谢提醒,但根据StackOverflow的规则,我无法将自己的答案标记为在两天内被接受。 – 2013-02-24 07:59:18