Here's the standard scenario:
if(string.IsNullOrEmpty(Configuration.AppSettings["foobar"])) throw new SomeStandardException("Application not configured correctly, bozo.");
The problem is that I'm not quite sure what exception should be SomeStandardException .
I looked through the 3.5 Framework and found two likely candidates: ConfigurationException and ConfigurationErrorsException .
System.Configuration.ConfigurationException
An exception that is thrown when a system configuration error has occurred.
Notes
A ConfigurationException if the application tries to read or write data to the configuration file, but to no avail. Some possible reasons for this may be incorrect XML in the configuration file, permission file, and configuration properties with values ββthat are not valid.Note:
A ConfigurationException object is supported for backward compatibility. ConfigurationErrorsException object replaces it for configuration.
This exception is really perfect for what I need, but it was deprecated, therefore, ixnay at atthay.
This brings us to a completely cryptic ConfigurationErrorsException :
System.Configuration.ConfigurationErrorsException
The current value is not one of the EnableSessionState Values.
As you can see, its documentation is completely useless. (This is true in both local and online help.) Studying the class itself shows that this is a radical overflow for what I want.
In short, I need a standard exception that should be thrown if the application configuration parameter is missing or contains an invalid value. You might think that there is such an exception in the Framework that is baked in it for use by applications. (Apparently, this was done, but it was obsolete and was replaced by something larger in volume.)
What solutions, if any, do you guys use for this, and will I have to suck it and roll my exception for this?
Edit uploads
Some ask if I can provide a default value and continue. In some cases, yes, and in those cases an exception will not be thrown. However, for certain settings this will not apply. For example: database server names and credentials, authentication servers, and paths to installed third-party applications.
It is also worth noting that the application that I am working on first of all is a console application that runs in batch mode, and I want it to throw an exception that was caught by the main method and registered accordingly if the thing is not properly configured . (This is the legacy code I inherited, and currently just accepts all peach.)