.NET: Which Exception to Throw When a Required Configuration Setting is Missing?

后端 未结 11 1142
星月不相逢
星月不相逢 2020-12-23 20:18

Here\'s a standard scenario:

if(string.IsNullOrEmpty(Configuration.AppSettings[\"foobar\"]))
   throw new SomeStandardException(\"Application not configured          


        
11条回答
  •  感动是毒
    2020-12-23 20:47

    The ConfigurationElement class (which is the base class of many config-related classes, like ConfigurationSection) has a method called OnRequiredPropertyNotFound (there are other helper methods too). You can maybe call those.

    The OnRequiredPropertyNotFound is implemented like this:

    protected virtual object OnRequiredPropertyNotFound(string name) {
        throw new ConfigurationErrorsException(SR.GetString("Config_base_required_attribute_missing", new object[] { name }), this.PropertyFileName(name), this.PropertyLineNumber(name)); }
    

提交回复
热议问题