C# RegexStringValidator未引发ArgumentException
我有以下代码:C# RegexStringValidator未引发ArgumentException,c#,visual-studio-2012,app-config,C#,Visual Studio 2012,App Config,我有以下代码: try { var configSection = config.GetSection("customSectionConfiguration") as CustomSection; } catch (ArgumentException ex) { throw new InvalidConfigurationException(ex); } catch (Exception) { throw; } 在我的CustomSection类中,我有一个Confi
try
{
var configSection = config.GetSection("customSectionConfiguration") as CustomSection;
}
catch (ArgumentException ex)
{
throw new InvalidConfigurationException(ex);
}
catch (Exception)
{
throw;
}
在我的CustomSection类中,我有一个ConfigurationElement,其属性上有RegexStringValidator属性,如下所示:
[ConfigurationProperty("property", IsRequired=true, DefaultValue="null")]
[RegexStringValidator("^\w*$")]
public string Property {...}
现在,在我的配置文件中,我放置了一个与此正则表达式冲突的值,当我尝试加载配置(config.GetSection(…)
)时,会按预期引发一个异常,但它不是RegexStringValidator文档所建议的ArgumentException。相反,它被通用异常捕获,并显示以下消息:“属性'property'的值无效。错误是:该值不符合验证正则表达式字符串'^\w*$'
有人知道实际抛出的异常类型吗?或者我如何在调试时确定这一类型?遵循Paul Griffin的建议后,发现此处抛出的异常类型是ConfigurationErrorsException。我在调试时通过在watch窗口中调用ex.GetType()发现了这一点。要快速检查,可以将泛型catch更改为
catch(异常ex)
并在catch块中放置一个断点,以调查ex的类型。这是我尝试过的,但我没有看到任何东西可以让我知道它是什么类型的异常。这次我在watch窗口中调用了ex.GetType(),解决了问题。事实证明这是一个配置错误异常。谢谢你的帮助。