9

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.

4
  • Have you considered having default using a default value instead with getProperty(String key, String defaultValue) ? Sep 9, 2015 at 12:17
  • @user61852 I'm not using default values at all.
    – ctomek
    Sep 9, 2015 at 13:01
  • Can't you simply write a helper method that calls getProperty() then does the null check?
    – Ixrec
    Sep 9, 2015 at 19:32
  • @Ixrec This idea is discussed under an accepted answer.
    – ctomek
    Sep 10, 2015 at 9:38

2 Answers 2

12

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.

8
  • It looks good. All properties must be present in a config file so it will be good solution. Thanks. As for now I cannot upvote, because I don't have enough reputation points, however I will wait with accepting this answer, because maybe more solutions will come, but for now yours looks ok.
    – ctomek
    Sep 9, 2015 at 10:57
  • Maybe simpler idea would be to just "write a helper method that calls getProperty() then does the null check" as @Ixrec suggested in a comment to my question?
    – ctomek
    Sep 10, 2015 at 7:13
  • @ctomek You could do that as well, but getProperty would be an ugly method that doesn't really belong to your class. If you're a purist, you would put it in its own class where it belongs and use that class, but it is also acceptable to create a helper method.
    – Neil
    Sep 10, 2015 at 7:17
  • Ok, so would you suggest to place Configuration class in the same file as class which is using it or in a separate file dedicatd only for Configuration class or in separate file with other helper / utility classes?
    – ctomek
    Sep 10, 2015 at 8:23
  • 1
    By InvalidParameterException did you mean java.security.InvalidParameterException or custom class? I am wondering what exception to throw.
    – ctomek
    May 20, 2016 at 12:50
2

The best solution would be to build a class Configuration which you initialize with either the location of the properties file, or a properties object, and then gets all the information it needs from that in the constructor, and throw an exception if it can't find the necessary information (or use a default value if you can use one).

This has the benefit that after startup, you know that you have all the values you need, and never have to check again. No need to spread the if statements all over your code.

Also, if you use defaults, you have them in one place, even if you use the setting in multiple places. Nice and DRY.

The not-null checking can be a utility notNull(value, message) method which returns value, allowing you to reduce if-statements.

Down-side: you end up with a God-object knowing all configuration names. This again can be solved by instantiating different SubComponentNameConfiguration objects for the specific sub-components.

PS: the real fun happens when certain properties mean "read some specific configuration from file at location value"

1
  • Thanks for pointing out that it is better to check everything at application startup. It's an answer at highest design level and it will also help, thanks.
    – ctomek
    Oct 24, 2015 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.