Java Map value access I am more or less a Java noob and wanted some feedback on a method I wrote. I wanted to avoid the pattern of checking if a key exists before accessing the value and throw an exception if the key does not exist.

Instead of doing this:

if (map.containsKey(key)) {
    return map.get(key)
}
else {
    throw new IllegalArgumentException();
}


everywhere in the code, I came up with the following solution:

class Utils {
    public static &lt;K, V&gt; V safeGet(final K key, Map&lt;K,V&gt; map) {
        if (map.containsKey(key)) {
            return map.get(key);
        } else {
            throw new IllegalArgumentException();
        }
    }
}


Is there anything wrong with this method? Do you see any problems with this approach?
As some other stated I am also convinced that there is less or no beneficial usage for enriching a simple associative map to throw an exception if an element is not found.

I suggest to go with a use case specific exception handling as the context is important where a not available value is an exceptional case.