I see, thanks!
Laertes
@Laertes
Best posts made by Laertes
Latest posts made by Laertes
-
RE: Correct error handling?
You got a valid point threre.
But your solution will also grow with the amount of preferences; although linear not exponential as in my case ;)
I wonder if there is a more generic way to do this, a solution that doesnt grow at all.
Like getting all critical preferences in a set, looping through all of them and build the string.
But to keep it generic, i'd need to get to the names of the variables somehow...Well listen to me thinking aloud ;)
Nevermind, i'll use your solution for now.Thanks
-
Correct error handling?
I wonder if this kind of error handling is WTFish or if it is a good way to do it.
public SomeClass(HashMap<String, String> configMap) throws CriticalPreferenceMissingException {
if(configMap.isEmpty()) {
throw new IllegalArgumentException("Configuration HashMap can't be emty.");
}
string1 = configMap.get("string1");
string2 = configMap.get("string2");
string3 = configMap.get("webappRoot");
integer1 = Integer.parseInt(configMap.get("integer1"));
boolean1 = Boolean.parseBoolean(configMap.get("boolean1"));
if(string1 == null || string2 == null || string3 == null) {
int errorCode =
string1 == null ? 1 : 0 +
string2 == null ? 2 : 0 +
string3 == null ? 4 : 0;
switch (errorCode) {
case 1:
throw new CriticalPreferenceMissingException("string1 not set.");
case 2:
throw new CriticalPreferenceMissingException("string2 not set.");
case 3:
throw new CriticalPreferenceMissingException("string1 and string2 not set.");
case 4:
throw new CriticalPreferenceMissingException("string3 not set");
case 5:
throw new CriticalPreferenceMissingException("string1 andstring3 not set");
case 6:
throw new CriticalPreferenceMissingException("string3 and string2 not set");
case 7:
throw new CriticalPreferenceMissingException("string1, string2 and string3 not set");
}
}
}