copyValueOf()—strongly exception-safe.


public void copyValueOf(
    Whole source) {
  PartOne t1 = rhs.p1.clone();

  if (null != t1) {
    try {
      PartTwo t2 = rhs.p2.clone();

      if (null != t2) {
        try {
          // examples of methods
          // that might throw
          t1.setParent(this);
          t2.setParent(this);

  // ********************************
       // This is the pivotal point of the
  // code - everything that could
  // fail is before this point.
  // Nothing that makes persistent
  // changes to the state of the
  // system is before this point.
// ********************************

          // The following commits
          // the change to the
          // system state.
          // Importantly it won't
          // throw.
          PartOne swap1 = t1;
          ti = p1; p1 = swap1;

          PartTwo swap2 = t2;
          t2 = p2; p2 = swap2;
        }
        finally {
          // either frees the
          // original resources or
          // of the temporary - 
          // depending whether we
          // passed the pivot 
          // uneventfully.
          t2.dispose();
        }
      }
    }
    finally {
      // either frees the original
      // resources or
      // of the temporary -
      // depending whether we
      // passed the pivot 
      // uneventfully.
      t1.dispose();
    }
  )
}

Featured

Most   Popular
Upcoming Events

AppTrends

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.