On Error Command
Obsolete
This command is obsolete. Please see the documentation listed in the See Also section for error handling in DataFlex.
Indicators are also obsolete, with the exception of predefined indicators that already exist in the language. Among the reasons for this is the limit of 89 user-defined indicators per program and that indicators are global.
Use the Boolean command to declare Boolean variables instead or declare properties in classes and objects. You can use the move command to set and retrieve the value of indicators.
Purpose
To execute a subroutine whenever an error occurs at runtime.
Syntax
on error
{
off | gosub
label
}
Label is the name of a subroutine in the program.
Off is a reserved word.
What It Does
On error gosub executes the subroutine, which you must provide in your program under label, to be executed whenever a non-fatal DataFlex error occurs at runtime. This allows you to provide corrective actions or user prompts in your program rather than relying only on the standard DataFlex error message to handle the situation. On error and on error off may be used as many times as desired in a program.
Your program may have any number of on error gosub commands, calling different subroutines, or sharing subroutines, but only the most recent on error gosub command is in effect at any given time. If you wish no on error gosub command to be in effect, use on error off.
on error gosub no_customer
In this example, subroutine no_customer is executed whenever a runtime error occurs after this command has been executed (so long as no subsequent on error command has been executed).
The Error-Handling Subroutine
The subroutine specified by the on error command is a true subroutine, that is, it must be located under a label (with a colon : at the end), and it must be terminated by a return command.
Inside your error handling subroutine, you may reference the line number on which the error occurred through the value of the errline predefined variable, and the number of the error triggered through the predefined variable lasterr. These can be used to fine-tune your error handling subroutine to handle specific errors.
Finally, your error handling subroutine should set the predefined indicator err to false. Err is set to true anytime an error is triggered. Subroutines called by on error gosub are not executed when err is true. This prevents the endless loop which would occur if an error occurred inside your subroutine, and it called itself. When an error is triggered while your subroutine is being executed, the normal (DataFlex) error routine is executed. The command indicate err false should be placed at the end of the subroutine, just prior to the return.
string discard 1
indicate err false
on error gosub wrong_disk
restart:
open datafile
command
:
command
abort
wrong_disk: // subroutine
if lasterr eq 75 begin // if error 75, do display
showln "DATAFILE.DAT isn't on the drive."
showln "Please insert the DATAFILE disk."
showln "Press <key> when done."
inkey discard // input a string
indicate err false
return restart // go to RESTART:
end
else error lasterr // if not 75, do normal MESSAGE
indicate err false
return // display other errors
This example shows an on error command together with the command (open datafile) whose error it is intended to trap, and the subroutine (wrong_disk), which executes in case the error occurs. Note that the on error command is above the open command whose error it is intended to trap. If the on error command were below the open command, errors triggered by the open command would execute before the on error command executed. If an error occurs, subroutine wrong_disk is executed. Wrong_disk allows all types of errors to execute normally except for Error 75 (Can't open data file (.DAT)). The if lasterr eq 75 executes a begin ... end block if that error occurs (likely as a result of the wrong disk being in the drive). If Error 75 occurs, a message advising the operator that the wrong disk is in the drive is displayed, and execution returns to label restart after the operator has changed disks and pressed a key. If the error is other than Error 75, the else error lasterr command executes the standard routine for that error, the next line sets predefined indicator err to false, and execution returns to wherever it was when the error occurred.
Notes
-
If you wish the routine for the triggered error to be executed as though unaffected by your subroutine (instead of trapped), provide the command
error lasterrin your subroutine, as shown above. -
You cannot use
on error gosubto trap window-entry Errors 13, 15, 17 (findreq), or 16 (date format). -
Always
indicate err falsebefore everyon error gosubcommand to guard againsterrhaving been left true by a previous error. -
In programs using procedures and functions in which error reporting is deferred, execution of an
on errorcommand when there is an error pending results in the reporting of the error.