VBScript error handling

When routines in the HCL Compass API encounter unexpected conditions, they throw an exception. If the exception is not caught by the calling program, the language interpreter terminates your program. If there is any possibility that the HCL Compass API call will fail, you should catch and handle exceptions.

Use the standard means of handling VBScript errors by using the VBScript On Error statement. You can then examine the Err error object and analyze errors at any time. For example:
On Error Resume Next
Err.Clear
' perform some operation here...
if  Err.Number <> 0  then   
   ' An exception occurred
    StdOut "Exception:" & vbCrLf &_
        "    Error number: " & Err.Number & vbCrLf &_
        "    Error description: '" & Err.Description & vbCrLf
	...
You can use a GoTo statement for Visual Basic but not for VBScript. For example:
' VB exception handling example
	On Error GoTo HandleError
	fieldValue = record.GetFieldStringValue(fieldname)
	...
	HandleError:
	StdOut "Got error number " & Err.Number & ":"
	StdOut Err.Description

Several functions which are expected to commonly fail are exceptions to this. In particular, validate and set field functions return error indications instead of throwing exceptions. For more information, see "Error checking and validation".

Handling VARIANT return values

For VBScript, some of the properties and methods return a VARIANT value which is supposed to contain an array of objects or Strings. If the array contains zero elements, then the VARIANT value would simply have a value of EMPTY. An empty value is not considered to be an array, and if you try to iterate over something that's not an array, it is considered a type-mismatch. You should check such a return value with the IsEmpty or IsArray functions before applying any array-related function on it. For example:


fieldObjs = GetInvalidFieldValues
' Check the return value
If (IsArray(fieldObjs)) Then 
   For Each fieldInfo In fieldObjs
      fieldValue = field.GetValue
      fieldName = field.GetName
      currentsession.outputdebugstring "This is the fieldvalue " & fieldvalue
   Next
Else
      currentsession.outputdebugstring "This is not an array or it is empty" 
End If