RE003 Violation of Uniqueness
Runtime Exceptions
noreferences

@@description

<h2>Umple runtime exception thrown when uniqueness is violated in a constructor</h2>

<p>The unique keyword in Umple can be applied to an attribute to ensure that each instance of the class in the currently running program has a unique value for that attribute. The code generated by Umple will not allow this constraint to be violated.</p>

<p>If an attempt is made to call the constructor of a class with an argument that would result in a violation of the uniqueness constraint, an exception will be thrown. The text of the constraint will start with the phrase &quot;Cannot create&quot;.</p>

<p>In the example below,
<ul>
<li>s1 is first initialized with number set to 1.

<li>Then an attempt is made to initialize s2 with number also set to 1. This throws the exception, but it is caught using a try-catch block.

<li>Next s3 is successfully created with number 2, but an attempt is made to change the number to 1 using the method call setNumber(). This returns false, since it is only constructors that throw the exception.

<li>Finally, an attempt is made to create s4, with the number set to 1. This throws the exception without a try-catch block, thus the program terminates.

</ul>
</p>

<p>Programs should be designed with logic that avoids violation of uniqueness. It is not recommended that production code relies on catching this exception as a normal part of its logic.</p>

@@example
@@source manualexamples/RE003UniquenessException.ump
@@endexample
