list of dots Digital Research Alliance of Canada logo  NSERC logo  University of Ottawa logo / UniversitĂ© d'Ottawa

User Manual    [Previous]   [Next]   

RE002 Violation of Association Multiplicity

Umple runtime exception thrown when multiplicity is violated in a constructor

Umple manages multiplicity by blocking operations that would allow too many or too few instances of a class to be linked. There are two ways in which links can be set, hence violating multiplicity: a) in a constructor, or b) after construction using an ordinary method. In a case of a constructor that would violate multiplicity, a RuntimeException is thrown. This can be caught by a try-catch block. After construction, any method that attempts to violate the multiplicity will return false.

In the first example below a student is always required to have a program. A runtime exception is thrown when an attempt is made to create a Student without specifying a required program. A method that attempt to violate multiplicity, and hence returns false, is shown at the end of the first example.

The second example shows a runtime exception that is thrown in the case of a one-to-one association. Since for such associations there must always be equal numbers of both associated classes, and objects of the two classes must be created in pairs, the constructor of either class also creates instances of the other class. An attempt to call such a constructor with a null argument will throw a runtime exception.

One to many example

// This demonstrates code that will throw the
// multiplicity exception
class Program {
  title;
  1 -- * Student;
}

class Student {
  name;
   
  public static void main(String[] argv) {
    
    Student s1 = null;
    Program p1 = null;

    // This will throw an exception since there must
    // be at least one program
    try {
      s1 = new Student("Abel", null);
    }
    catch (RuntimeException re) {
      System.out.println("Exception Caught: "+re+"\n");    
    }
    
    // After we create a program we can repeat the
    // attempt.
    p1 = new Program("Computer Science");
    s1 = new Student("Abel", p1);
    
    // Attempting to change the program back to null
    // will not throw an exception but will result
    // in null being thrown
    if(!s1.setProgram(null)) {
      System.out.println("Could not set program "+
       "to null as would violate multiplicity \n");
    }
  }
}

      

Load the above code into UmpleOnline

 

One to one example

// This demonstrates code that will throw a
// multiplicity exception in  1--1 associations
class StockSymbol {
  code;
  1 -- 1 Company;
}

class Company {
  name;
   
  public static void main(String[] argv) {
    
    Company c1 = null;
    StockSymbol s1 = null;

    // This will throw an exception since the stock
    // symbol cannot be null
    try {
      c1 = new Company("Umpleco", s1);
    }
    catch (RuntimeException re) {
      System.out.println("Exception Caught: "+re+"\n");    
    }    
    
    // This will succeed: Umple 1--1 constructors
    // create both objects at once
    c1 = new Company("Umpleco","UMPC");
    
    s1 = c1.getStockSymbol();
    System.out.println("The company: "+
      c1+"\n"+
      "The stock symbol: "+
      s1+"\n");     
  }
}

      

Load the above code into UmpleOnline