对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中设置的异常