RegexStringValidator 不抛出 ArgumentException
RegexStringValidator not throwing ArgumentException
我有以下代码:
try
{
var configSection = config.GetSection("customSectionConfiguration") as CustomSection;
}
catch (ArgumentException ex)
{
throw new InvalidConfigurationException(ex);
}
catch (Exception)
{
throw;
}
在我的 CustomSection class 中,我有一个 ConfigurationElement,它的属性带有 RegexStringValidator 属性,如下所示:
[ConfigurationProperty("property", IsRequired=true, DefaultValue="null")]
[RegexStringValidator("^\w*$")]
public string Property {...}
现在,在我的配置文件中,我放置了一个违反此正则表达式的值,当我尝试加载配置 (config.GetSection(...)
) 时,会按预期抛出异常,但它不是 RegexStringValidator 文档建议的 ArgumentException。相反,它被带有此消息的通用异常捕获:"The value for the property 'property' is not valid. The error is: The value does not conform to the validation regex string '^\w*$'".
有谁知道实际上抛出了什么类型的异常,或者我如何在调试时确定它?
在听从 Paul Griffin 的建议后,发现这里抛出的异常类型是 ConfigurationErrorsException。我在调试时通过在手表 window 中调用 ex.GetType() 发现了这一点。
我有以下代码:
try
{
var configSection = config.GetSection("customSectionConfiguration") as CustomSection;
}
catch (ArgumentException ex)
{
throw new InvalidConfigurationException(ex);
}
catch (Exception)
{
throw;
}
在我的 CustomSection class 中,我有一个 ConfigurationElement,它的属性带有 RegexStringValidator 属性,如下所示:
[ConfigurationProperty("property", IsRequired=true, DefaultValue="null")]
[RegexStringValidator("^\w*$")]
public string Property {...}
现在,在我的配置文件中,我放置了一个违反此正则表达式的值,当我尝试加载配置 (config.GetSection(...)
) 时,会按预期抛出异常,但它不是 RegexStringValidator 文档建议的 ArgumentException。相反,它被带有此消息的通用异常捕获:"The value for the property 'property' is not valid. The error is: The value does not conform to the validation regex string '^\w*$'".
有谁知道实际上抛出了什么类型的异常,或者我如何在调试时确定它?
在听从 Paul Griffin 的建议后,发现这里抛出的异常类型是 ConfigurationErrorsException。我在调试时通过在手表 window 中调用 ex.GetType() 发现了这一点。