C# 如何从Web.config修改异常处理值
我开始使用异常处理,在我的例子中,我使用配置管理器,使用替换处理程序创建空策略,在这种情况下,替换到此异常的消息是下一条:“存在一些空值:”…my web.config:C# 如何从Web.config修改异常处理值,c#,exception,enterprise-library,C#,Exception,Enterprise Library,我开始使用异常处理,在我的例子中,我使用配置管理器,使用替换处理程序创建空策略,在这种情况下,替换到此异常的消息是下一条:“存在一些空值:”…my web.config: <exceptionHandling> <exceptionPolicies> <add name="Null Policies"> <exceptionTypes> <add name="All Exceptions
<exceptionHandling>
<exceptionPolicies>
<add name="Null Policies">
<exceptionTypes>
<add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="ThrowNewException">
<exceptionHandlers>
<add name="Replace Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
exceptionMessage="Exists some null values" replaceExceptionType="System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
通过研究我发现了访问web.config的
Configuration
类……我不知道如何访问这段特定的XML->ExceptionPolicys
是否有特定的原因需要使用您描述的方法?如果您只想添加有关异常的附加信息,那么可以通过创建并引发新异常来完成,将原始异常指定为内部异常。例如,在您的catch中,“抛出新异常(“有空值-”+someOtherInfo,ex);”。原始异常及其stacktrace将通过您抛出的异常的InnerException属性可用。您不应该“处理”NullReferenceException。你应该修复导致它的bug。
try
{
string theValue = anotherThing //this piece of code throw a NullReferenceException
}
catch(Exception ex)
{
if (exManager.HandleException(ex,"Null Policies")) throw; //add 'theValue' to the new Exception
return "";
}