Enterprise Library 3.1 异常应用程序块

对Enterprise Library 3.1 中的 异常应用程序块进行了一个小的调查

PostHandlingAction 中有 None,NotifyRethrow,ThrowNewException三项
None: 返回false,不抛出新的异常
NotifyRethrow: 返回True,不抛出新的异常
ThrowNewException:抛出新的异常(新异常取决与Handler的设定

例如:

Try

            
Dim  ex  As  Exception  =   New  Exception

            
Throw  ex

        
Catch  ex  As  Exception

            Debug.WriteLine(
" Exception Policy " )

            
Try

                Debug.WriteLine(
" Before Exception  =  "   &  ex.GetType().ToString)

                
Dim  b  As   Boolean   =  ExceptionPolicy.HandleException(ex,  " Exception Policy " )

                
If  b  Then
                    Debug.WriteLine(b.ToString)
                
End   If

            
Catch   exp   As  ApplicationException

                Debug.WriteLine(
" After Exception  =  "   &   exp .GetType().ToString)

                Debug.WriteLine(
" ApplicationException " )

            
End   Try

        
End   Try

Exception Policy的设置如下

< add  name ="Exception Policy" >
        
< exceptionTypes >
          
< add  type ="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction
="None"  name ="Exception" >
            
< exceptionHandlers >
              
< add  logCategory ="General"  eventId ="100"  severity ="Error"  title ="Exception Handling For Log"
                formatterType
="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                priority
="3"  type ="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                name
="Logging Handler"   />
              
< add  exceptionMessage ="##### System.ApplicationException #####"
                exceptionMessageResourceType
=""  replaceExceptionType ="System.ApplicationException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                type
="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                name
="Replace Handler"   />
            
</ exceptionHandlers >
          
</ add >
        
</ exceptionTypes >
      
</ add >

其中的Replace Handler将原先的Exception替换为ApplicationException
当PostHandlingAction为None时,打印以下信息

'System.Exception' の初回例外が Enterprise Library.exe で発生しました。
Exception Policy
Before Exception  = System.Exception

没有抛出新的异常,并且返回值也是false

当PostHandlingAction为NotifyRethrow时,打印以下信息

'System.Exception' の初回例外が Enterprise Library.exe で発生しました。
Exception Policy
Before Exception  = System.Exception
True

没有抛出新的异常,但是返回值是True
当PostHandlingAction为ThrowNewException时,打印以下信息

'System.Exception' の初回例外が Enterprise Library.exe で発生しました。
Exception Policy
Before Exception  = System.Exception
'System.ApplicationException' の初回例外が Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll で発生しました。
After Exception  = System.ApplicationException
ApplicationException

在执行

Dim  b  As   Boolean   =  ExceptionPolicy.HandleException(ex,  " Exception Policy " )

该句的时候抛出新的异常ApplicationException,也就是在ReplaceHandler中设置的异常

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值