Skip to content
Advertisement

Throw exception when a property is duplicated in a properties file

How can I throw an Exception when a properties file contains a duplicate property? Here is an example demonstrating this situation:

JavaScript

Advertisement

Answer

I suppose you are reading the file with something like Properties.load(). It sets the parameter internally using put(key, value). You can override that method to get the desired behaviour like e.g.

JavaScript

EDIT:

Integrating this into the OP’s code:

JavaScript

By the way, why would you want to catch the exception? I’d not continue a program if its configuration is corrupt (maybe catching at top-level to log the event). But exception-handling is a different topic…

(EDIT: my original code samles didn’t compile, I corrected them)

Advertisement