N
N
NoMoneyException2017-04-13 09:25:10
Java
NoMoneyException, 2017-04-13 09:25:10

Should I handle errors when using ResourceBundle?

Hello. I took a ready-made piece of code that will read the property file with the database config.

public class DBResManager {
    private final String CONFIG_PATH = "res.dbconfig";
    private final static DBResManager instance = new DBResManager();

    private ResourceBundle dbConfig = ResourceBundle.getBundle(CONFIG_PATH);

    private DBResManager() {

    }

    public static DBResManager getInstance() {
        return instance;
    }

    public String getValue(String key) {
        return dbConfig.getString(key);
    }
}

It seems to work, but what if the filename changes or I throw in a key that isn't in the property file? The exception will crash, but why then its processing is not required? Or is it still worth trying? Thanks :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene Khrustalev, 2017-04-13
@eugenehr

Properties files are internal application settings (variables). They are not edited from the outside, which means they must always contain correct data. If the properies file does not contain the key that the application requests, then this is either an incorrect file or errors in the program. It is better to exclude this at the development / testing stage, for which it is best to throw an exception.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question