Constructing an Exception

Exceptions1are typically created with a catalog name and message identifier. If these are not specified default values are used. The server infrastructure will take care of delivering the message text to the client and/or log file. For example:

Figure 1. Constructing an AppException
if (DatabaseFieldIsNull()) {
  curam.util.exception.AppException e = new
     AppException(MAINTENANCE.ID_NULL_INDICATOR);
  throw e;
}

// This can also be written as follows
if (DatabaseFieldIsNull() ) {
  throw new curam.util.exception.AppException
         (INFRASTRUCTURE.ID_NULL_INDICATOR);
}

The purpose of exceptions is to communicate the fact that an error has occurred and to communicate information about that error. Often it is necessary to include additional information as well as the error code. This can be done using arguments.

Arguments are attached to an exception before it is thrown and are intended to be ultimately included in the error message displayed at the client and/or the server log file.

To attach an argument to an exception, the arg method (.arg()) is used. Constructing an Exception shows a code example of how to use the arg method to attach an argument to an exception.

Figure 2. Using the arg method with a primitive type
// set a status code for the error which occurred
long lngErrorCode = -1;

// create the exception.
curam.util.exception.AppException e = new
          AppException(MAINTENANCE.ID_SYSTEM_ERROR);

// Include this status code with the exception.
e.arg(lngErrorCode);

// now throw the exception
throw e;

The arg method supports the addition of many different types of arguments to an exception. Such primitive types include long, boolean or double while complex types e.g. Date, DateTime, Money and CodeTableItemIdentifier objects can also be added. See the JavaDoc for curam.util.exception.AppException for more details.

Figure 3. Using the arg method with a complex type
// Create a codetable identifier to describe domain type.
curam.util.type.CodeTableItemIdentifier aCodeIdentifier =
  new CodeTableItemIdentifier
    (DOMAINTYPE.TABLENAME, DOMAINTYPE.INT32);

// create the exception to flag an invalid data type
curam.util.exception.AppException e = new
  AppException(WORKFLOW.ERR_ANSWER_NOT_VALID_DATATYPE);

// Include the domain type code with the exception.
e.arg(aCodeIdentifier);

// now throw the exception
throw e;
1 The following sections focus on use of AppException rather than AppRuntimeException as this is typical of production code. However, AppRuntimeException can be created and manipulated in the same way.