Try OpenEdge Now
skip to main content
Object-oriented Programming
Programming with Class-based Objects : Raising and handling error conditions : Raising errors within a property : Property error handling example
 
Property error handling example
The following code fragment from the sample class, acme.myObjs.CreditObj, shows an example of property error handling:
ROUTINE-LEVEL ON ERROR UNDO, THROW.
USING acme.myObjs.Common.*.

CLASS acme.myObjs.CreditObj:
  DEFINE PUBLIC PROPERTY CustCreditLimit AS DECIMAL INITIAL ? NO-UNDO
    /* GET: Returns the credit limit of the current Customer.       */
    /* If there is no current Customer, it returns Unknown (?).     */
    GET.
    /* SET: Raises the credit limit for Customers in good standing. */
    /* Current increase is $1,000.                                  */
    PROTECTED SET (INPUT piCL AS DECIMAL):
      IF Customer.Balance > piCL THEN DO:
        CustCreditLimit = Customer.Creditlimit.
        UNDO, THROW NEW Progress.Lang.AppError("Over Limit").
      END.
      ELSE
       ASSIGN
         Customer.Creditlimit = piCL + 1000.
         CustCreditLimit      = Customer.Creditlimit.
    END SET.
   ...

  METHOD PUBLIC VOID CheckCustCredit ( ):
    /* Invokes the CustCreditLimit property SET accessor */
    IF AVAILABLE (Customer) THEN
      CustCreditLimit = Customer.Creditlimit.
    ELSE
      UNDO, THROW NEW Progress.Lang.AppError( "No Customer" ).
  END METHOD.
END CLASS.
This class defines the CustCreditLimit property to set the credit limit for the current Customer record, depending on the customer’s credit standing. The SET accessor raises ERROR by throwing an application error object if the customer’s balance is over their current credit limit, by first setting the property value to that current limit, then creating the error object with a constructor that specifies an error return string ("Over Limit") to indicate this condition. For this error condition, note also that the SET accessor retains its setting because the property is defined as NO-UNDO, and it automatically throws the error object out of the accessor block to (raises ERROR on) the statement that sets the property value because the class is defined with the ROUTINE-LEVEL ON ERROR UNDO, THROW statement. However, if the customer’s credit standing is good, the SET accessor immediately raises the customer’s credit limit and sets the property with that new value.
So, as defined in the same class, the CheckCustCredit( ) method sets the CustCreditLimit property with the customer’s current credit limit and automatically re-throws any error object to the caller that is thrown from setting the property value. This error object is, in turn, caught and checked by the CheckCredit( ) method in the sample class, acme.myObjs.CreditObj.