@IfError (Formula Language)

Returns a null string ("") or the value of an alternative statement if a statement returns an error.

Note: This @function is new with Release 6.
Note: This @function is obsolete in Release 7.

Syntax

@IfError( statement1 ; statement2 )

Parameters

statement1

A formula statement. This statement executes first.

statement2

Optional. A formula statement. This statement, if available, executes if the first statement returns an error.

Return value

statementReturn

  • Returns the value of the first statement if it is not an error.
  • Returns the value of the second statement if the value of the first statement is an error and the second statement is supplied.
  • Returns a null string ("") if the value of the first statement is an error and the second statement is omitted.

Usage

Use $Error in the second statement to get the value of the error.

This command should be replaced with the following series of commands.

result := statement1;
@If(@IsError(result);statement2;result)

Since this function intercepts the error message and replaces it with your own value, if you do have an error, you may have trouble figuring out what's causing the error. For debugging purposes, you may want to temporarily remove the error handling so that you can see the error message text or, display the text as shown in example 4.

Examples

  1. This agent tests the return value of an @DbLookup statement for an error. If the @DbLookup statement causes an error, the agent returns the text "Not available."
    FIELD Phone :=
    @IfError(
    @DbLookup(""; "Snapper" : "names.nsf"; "People";
    @Right(Name; " ") + " , " + @Left(Name; " "); "OfficePhoneNumber");
    "Not available")

    This agent does the same thing, using @If instead of @IfError.

    result := @DbLookup("";"Snapper":"names.nsf";"People";
    @Right(Name;" ") + " , " + @Left(Name; " "); "OfficePhoneNumber");
    FIELD Phone := @If(@IsError(result);"Not available";result)

  2. The following code, when added to a Computed for display field, displays the price of the product entered in the "product" field, after a page refresh. Enter the text, "Enter product name here" as the default value for the product field. Once a user enters a product name in the product field and presses F9, the price is extracted from the Goods view, which contains the product name in the first sorted column and its price in the second column. If the product name is not recognized or any other error occurs during the lookup, the message, "Unable to retrieve requested price. Aborting lookup" displays. You could add a Get Price action button that contains the code: @Command([ViewRefreshFields]) to prompt the user to refresh the page.
    @If(product="Enter product name here";0;@IfError(@DbLookup("" : "" ; "product/server" : "filename\\productdatabase.nsf" ; "Goods" ; product ; 2); "Unable to retrieve requested price. Aborting lookup"))
  3. This formula, when added to the "Apply font" hotspot button, applies the font a user selects from the "fonts" Dialog list field to the text the user enters or highlights in the "Body" Rich Text field. The "fonts" field contains an @FontList function in the Use formula for choices box in its Field Properties box, which displays a list of available fonts. If no font was selected from the "fonts" field, an error message displays which instructs the user to select one.
    @Command([EditGoToField]);"Body");
    @Command([EditSelectAll]);
    @IfError(@Command([TextSetFontFace];fonts);@Prompt([Ok];"Error encountered";"You must select a font first"))
  4. This returns the lookup result if there is one, but if the lookup fails, it returns the text of the error message without causing an error condition. This may be useful in debugging.
    @IfError(@DbLookup("":"NoCache"; ""; "ById"; ID; 2); 
    @Text($Error))