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

后端 未结 11 1138
星月不相逢
星月不相逢 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: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.

提交回复
热议问题