Multiple On Error statements

Handling individual errors

An On Error statement refers to only one error-handling routine. For more than one, you include multiple On Error statements, one for each error-handling routine in your script.

For example:

You include a Print statement in a script that can generate a division-by-zero error. To handle a division-by-zero error, you could include an On Error statement that specifies this error and designates an error-handling routine that responds appropriately to the error. The routine begins at the DivZero label. It includes an InputBox$ function call that prompts the user to type a replacement value for the 0 (zero) that was read from the opened file. The additional On Error statement is

  On Error ErrDivisionByZero GoTo DivZero

The error-handling routine looks like this:

DivZero:
   number3% = InputBox$("Number3 is 0. Enter a new value: ")
   ' Resume execution with the statement that caused
   ' the error ErrDivisionByZero.
   Resume

To ensure that all other errors are handled without terminating script execution, include an On Error statement that doesn't specify a particular error.

This example shows a script that specifically manages file-open failures and division-by-zero errors. All others are included in a general On Error statement.

%Include "lserr.lss"
Sub GetLine
   Dim number1 As Integer, number2 As Integer, number3 _
    As Integer
   Dim fileName As String
   ' The error-handling routine at label Leave is for
   ' all errors except the two individual errors
   ' specified in the second and third On Error statements.
   ' Each has a specific error-handling routine designated.
   On Error GoTo Leave
   On Error ErrOpenFailed GoTo NoExist
   On Error ErrDivisionByZero GoTo DivZero
GetName:
   fileName$ = InputBox$("Enter a file name: ")
   Open fileName$ For Input As #1
   Input #1, number1%, number2%, number3%
   Print number1%, number2%, number3%
' The next statement causes a division-by-zero error if
' number 3 is 0.
   Print (number1% + number2%) / number3% 
   Close #1
   Exit Sub
NoExist:
   Print Error(), Err(), Erl()
   Resume GetName
DivZero:
   number3% = InputBox("Number3 is 0. Enter a new value: ")
   Resume
Leave:
   ' The following message is general, because different
   ' errors may have occurred.
   MessageBox("Cannot complete operation.")
   Exit Sub
End Sub

This example of a call to GetLine shows how the sub works. For all errors other than file-open failures errors and division-by-zero errors, the error-handling routine at Leave displays a message box and returns from the sub GetLine.

Call GetLine()
' The user enters a valid file name, and the values read in
' from the file are 11, 22, and 0.
' Output:
' 11           22            0
' The value 0 causes a division-by-zero error.
' The user then enters the value 2 into the input box
' specified in the error-handling routine beginning at
' DivZero. Execution resumes at the Print statement that
' generated the error.
' Output: 16.5

However, if the user enters 99999 instead of 2 into the input box in the error-handling routine at DivZero, the result is an overflow error, because 99999 is larger than the maximum legal Integer value for the variable number3%. This error will not be handled, because it occurs within the error-handling routine at DivZero. LotusScript® ends execution whenever an error occurs within an error-handling routine.

Ordering of On Error statements

The error-handling routine (or none) in effect at any given time for any particular error is the routine specified in the most recently executed On Error statement that applies to that error. Changing the order of the On Error statements can change the processing at run time.

In this example, the order of the three On Error statements at the beginning of the preceding example is changed to this:

' Two routines are designated to handle individual errors.
On Error ErrOpenFailed GoTo NoExist
On Error ErrDivisionByZero GoTo DivZero
' The Leave routine is intended to handle all other errors.
On Error GoTo Leave

After these three statements execute, all errors are handled by the error-handling routine beginning at the label Leave, because the statement On Error GoTo Leave refers to all errors. The routine named Leave overrides the routines established for ErrOpenFailed and for ErrDivisionByZero that were specified in the preceding two On Error statements.