title: .NET: Which Exception to Throw When a Required Configuration Setting is Missing?
body: <p>Here's a standard scenario:</p>
  
  <pre><code>if(string.IsNullOrEmpty(Configuration.AppSettings["foobar"]))
     throw new SomeStandardException("Application not configured correctly, bozo.");
  </code></pre>
  
  <p>The problem is, I am not entirely certain <em>which</em> exception <code>SomeStandardException</code> should be. </p>
  
  <p>I perused the 3.5 Framework and found two likely candidates: <a href="http://msdn.microsoft.com/en-us/library/system.configuration.configurationexception.aspx" rel="noreferrer"><code>ConfigurationException</code></a> and <a href="http://msdn.microsoft.com/en-us/library/system.configuration.configurationerrorsexception.aspx" rel="noreferrer"><code>ConfigurationErrorsException</code></a>.</p>
  
  <blockquote>
    <h2>System.Configuration.ConfigurationException</h2>
    
    <p>The exception that is thrown when a
    configuration system error has
    occurred.</p>
    
    <p><h3>Remarks</h3> The
    <code>ConfigurationException</code> exception is
    thrown if the application attempts to
    read or write data to the
    configuration file but is
    unsuccessful. Some possible reasons
    for this can include malformed XML in
    the configuration file, file
    permission issues, and configuration
    properties with values that are not
    valid.</p>
    
    <p><strong>Note:</strong></p>
    
    <p>The <code>ConfigurationException</code> object is
    maintained for backward compatibility.
    The <code>ConfigurationErrorsException</code>
    object replaces it for the
    configuration system.</p>
  </blockquote>
  
  <p>This exception actually sounds perfect for what I need, but it's been marked obsolete, so, ixnay on atthay.</p>
  
  <p>This brings us to the thoroughly puzzling <a href="http://msdn.microsoft.com/en-us/library/system.configuration.configurationerrorsexception.aspx" rel="noreferrer"><code>ConfigurationErrorsException</code></a>:</p>
  
  <blockquote>
    <h2>System.Configuration.ConfigurationErrorsException</h2>
    
    <p>The current value is not one of the
    EnableSessionState values.</p>
  </blockquote>
  
  <p>As you can see, its documentation is completely useless. (It's that way in both local and online help.) An examination of the class itself shows that it's drastic overkill for what I want. </p>
  
  <p>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 <em>much</em> larger in scope.)</p>
  
  <p>What solutions, if any, are you guys using for this, and am I going to have to suck it up and roll my own exception for this?</p>
  
  <h2>Edit Addenda</h2>
  
  <p>Some have asked whether or not I could provide a default value, and continue. In certain cases, yes, and in those cases, the exception would not be thrown. However, for certain settings, this won't apply. For instance: database server names and credentials, authentication servers, and paths to installed third party applications. </p>
  
  <p>It is also worth noting that the application I'm primarily working on is a console application running in batch mode, and I want it to throw an exception that is caught by the main method and logged appropriately if the thing isn't appropriately configured. (It's legacy code I've inherited, and currently just <em>assumes</em> everything's peachy.)</p>