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

后端 未结 11 1082
星月不相逢
星月不相逢 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)); }
    
    0 讨论(0)
  • 2020-12-23 20:50

    As Daniel Richardson said, ConfigurationErrorsException is the one to use. In general it is only recommended to create your own custom Exception types if you have a scenario to handle them. In the case of configuration errors, which are usually fatal, this is rarely the case so it's usually more appropriate to reuse the existing ConfigurationErrorsException type.

    Prior to .NET 2.0, the recommendation was to use System.Configuration.ConfigurationException. ConfigurationException became obsolete in .NET 2.0, for reasons which were never clear to me, and the recommendation changed to use ConfigurationErrorsException.

    I use a helper method to throw the exception so that it's easy to change the exception being thrown in one place when migrating from .NET 1.x to 2.0, or if Microsoft decides to change the recommendation again:

    if(string.IsNullOrEmpty(Configuration.AppSettings("foobar")))
    {
       throw CreateMissingSettingException("foobar");
    }
    
    ...
    
    private static Exception CreateMissingSettingException(string name)
    {
        return new ConfigurationErrorsException(
            String.Format
            (
            CultureInfo.CurrentCulture,
            Properties.Resources.MissingConfigSetting,
            name
            )
            );
    }
    
    0 讨论(0)
  • 2020-12-23 20:53

    What about System.Configuration.SettingsPropertyNotFoundException?

    0 讨论(0)
  • 2020-12-23 20:53

    I tend to disagree with the premise of your question:

    In a nutshell, I need a standard exception that should be thrown when an application configuration setting is missing or contains an invalid value. You'd think the Framework had such an exception baked into it for applications to use. (It apparently did, but it was marked obsolete, and was replaced by something much larger in scope.)

    According to the MSDN documentation on System.Exception (Exception Class, you really shouldn't be throwing exceptions for user input errors, for performance reasons (which has been pointed out by others on Stack Overflow and elsewhere). This seems to make sense as well - why can't your function return false if the user input is entered incorrectly, and then have the application gracefully exit? This seems to be more of a design problem then an issue with which Exception to throw.

    As others have pointed out, if you really have to thrown an exception - for whatever reason - there isn't any reason why you couldn't define your Exception type by inheriting from System.Exception.

    0 讨论(0)
  • 2020-12-23 20:55

    ConfigurationErrorsException is the correct exception to throw in the situation you describe. An earlier version of the MSDN documentation for ConfigurationErrorsException makes more sense.

    http://msdn.microsoft.com/en-us/library/system.configuration.configurationerrorsexception(VS.80).aspx

    The earlier MSDN summary and remarks are:

    • The exception that is thrown when a configuration-system error has occurred.
    • The ConfigurationErrorsException exception is thrown when any error occurs while configuration information is being read or written.
    0 讨论(0)
提交回复
热议问题