Simple question: What is the best/common practice regarding to throwing errors for application configuration providers?
Given is a simple key/value-based configuration source:
class Configuration
string GetValue(key);
void SetValue(key, value);
Or more extended, with the ability to add/remove key/value elements
class Configuration
string GetValue(key);
void SetValue(key, value);
void AddKeyValuePair(string key, string value);
void RemoveKeyValuePair(string key);
Not throwing exceptions on methods like GetValue()
has the benefit of not interrupting the application flow and making code easier to read because of a lack of exception handling etc. It also allows to use null-conditional operators (maybe monads) or null-coalescing operators for e.g. setting defaults:
var value = configuration.GetValue("myKey") ?? "Hello World";
Return null
simply tells the user: 'Whoops, got nothing to return since nothing is set/there'.
However..
If I'd avoid throwing exceptions, what would happen if I try to add a key/value pair which is already available? Or set the value with a key that isn't there in the underlying storage? Or removing a key/value pair that isn't available? Would it be obvious for the user that simply nothing happens when we tries to remove a none-existing key pair?
What is the prefered approach for application configuration?