it's not a big programming problem, but I'm curious if it should be done better. So I have a config file which I'm reading via java.util.Properties class. When property is not present in a file getProperty() method returns null. I would like to throw an exception if any property is not present in config file. So how should I do it?Two ideas comes to mind, but I think there should be other way to do it. The first is that after every call of getProperty() I would do if (prop == null) throw new SomeException, but it wouldn't look nice if there would be identical ifs after every two lines. Second approach is to make one big if after all properties are read and just check them all at once.
Throwing an exception if some property is not present in a properties file
java;properties
Third option is that you create a derived class of Properties that does this for you:public class Configuration extends Properties {    public String getProperty(final String key) {        final String property = super.getProperty(key);        if(property == null) {            throw new InvalidParameterException(MessageFormat.format(Missing value for key {0}!, key)));        }        return property;    }    public String getProperty(final String key, final String defaultValue) {        // Throw exception here as above if you want to throw exception even with defaultValue        return super.getProperty(key, defaultValue);    }}I call it Configuration because it is a scenario in which missing properties might be cause to throw an exception, though you're welcome to call it as you wish.  As it is now, calling getProperty given parameter defaultValue will not throw an exception (since presumably you're okay with a value not being there).  On a side note, you should ask yourself if this is how you truly want to handle this.  It may happen that there aren't values that you expect to find in a property file.  In that case there is nothing exceptional about it.  However if you expect to find very specific values that will cause the application to fail if they aren't present, then this solution is satisfactory.  I hope that helps.