Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
ON ERROR Statement Details
◄Summary► ◄Details► ◄Example► ◄Contents► ◄Index► ◄Back►
──────────────────────────────────────────────────────────────────────────────
ON [LOCAL] ERROR {GOTO line | RESUME NEXT | GOTO 0}
Usage Notes
■ If no ON ERROR statement is used in a program, any run-time error will
be fatal. Visual Basic generates an error message and program execution
stops.
■ The LOCAL keyword is used to indicate an error-handling routine that is
"local" to the procedure in which the error handler is located. A local
error-handling routine:
• Overrides any enabled module-level error-handling routines
• Is enabled only while the procedure it is located in is executing
■ The local error handler remains enabled while any procedures execute
that are directly or indirectly called by the procedure that contains
the error handler. Error handlers within those procedures will supersede
the local error handler.
■ If an error occurs in a procedure or module that does not have an
enabled error-handling routine, Visual Basic searches for an enabled,
inactive error handler to trap the error. Visual Basic will search back
through all the procedures and modules that have called the procedure
where the error occurred. In a multiple-module program, this search
includes error handlers at the module level of different modules.
■ The argument line is either in the module-level code, or, if the LOCAL
keyword is used, in the same procedure. If not found in either place,
Visual Basic generates the compile-time error message, "Label not
defined."
■ The RESUME NEXT option causes program execution to resume with the
statement immediately following the statement that caused the run-time
error. Use RESUME NEXT to either:
• Continue execution of a block of code despite a run-time error, then
check for the cause of the error
• Build the error-handling code in line with the main program or
procedure, rather than at a remote location in the program
■ ON ERROR GOTO 0 disables error handling. It does not specify line 0
as the start of the error-handling code, even if the program or
procedure contains a line numbered 0.
■ An error-handling routine is not a SUB or FUNCTION procedure or a DEF FN
function. An error-handling routine is a block of code marked by a line
label or line number that ends with a RESUME statement.
■ An error-handling routine will not be executed unless:
• It is enabled by execution of an ON ERROR GOTO line statement
• A fault in the structure of the program causes program control to
branch to or fall into the error-handling code
• An error statement is used to cause an error condition
■ Once an error handler is enabled, if a run-time error that can be
trapped occurs, then program control jumps to the enabled error-
handling routine, and the error handler becomes "active."
■ An error handler is active from the time a run-time error has been
trapped until a RESUME statement is executed in the handler.
■ Error-handling routines must rely on the value in ERR to determine
the cause of the error. The error-handling routine should test or save
this value before any other error can occur or before calling a
procedure that could cause an error. The value in ERR reflects only
the last error that occurred.
■ If you use the BC command line to compile a program that has been
developed in the Visual Basic programming environment (VBDOS.EXE) and
has error-handling routines, compile with the /E or /X option. The VBDOS
Make EXE command uses these options.
See: ◄BC Command-Line Options►
See: ◄Using Error Handlers►