I think this check should come before the throw, so that if an existing key/value pair is added, no error is thrown.