Here's a standard scenario:
if(string.IsNullOrEmpty(Configuration.AppSettings("foobar")))
throw new SomeStandardException("Application not configured correctly, bozo.");
The problem is, I am not entirely certain which exception SomeStandardException
should be.
I perused the 3.5 Framework and found two likely candidates: ConfigurationException
and ConfigurationErrorsException
.
System.Configuration.ConfigurationException
The exception that is thrown when a configuration system error has occurred.
Remarks
TheConfigurationException
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.Note:
The
ConfigurationException
object is maintained for backward compatibility. TheConfigurationErrorsException
object replaces it for the configuration system.
This exception actually sounds perfect for what I need, but it's been marked obsolete, so, ixnay on atthay.
This brings us to the thoroughly puzzling ConfigurationErrorsException
:
System.Configuration.ConfigurationErrorsException
The current value is not one of the EnableSessionState values.
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.
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.)
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?
Edit Addenda
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.
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 assumes everything's peachy.)