Enables an error-handling routine and specifies the location of the routine; can also be used to disable an error-handling routine.
On Error GoTo line On Error Resume Next On Error GoTo 0 |
The On Error statement syntax can have any of the following forms:
Statement | Description |
On Error GoTo line | Enables the error-handling routine that starts at line specified
in the required line argument. The line argument is any line
label or line number. If a run-time error occurs, control branches to line,
making the error handler active. The specified line must be in the
same procedure as the On Error statement; otherwise, a compile-time
error occurs. If the On Error statement is in the local area of visibility, the specified specified line should be defined in the same area. |
On Error Resume Next | Specifies that when a run-time error occurs, control goes to the statement immediately following the statement where the error occurred where execution continues. Use this form rather than On Error GoTo when accessing objects. |
On Error GoTo 0 | Disables any enabled error handler in the current procedure. |
If you don't use an On Error statement, any run-time error that occurs
is fatal; that is, an error message is displayed and execution stops.
An "enabled" error handler is one that is turned on by an On Error
statement; an "active" error handler is an enabled handler that is
in the process of handling an error. If an error occurs while an error handler
is active (between the occurrence of the error and a Resume, Exit
Sub or Exit Function statement), the current procedure's error handler
can't handle the error. Control returns to the calling procedure. If the calling
procedure has an enabled error handler, it is activated to handle the error.
If the calling procedure's error handler is also active, control passes back
through previous calling procedures until an enabled, but inactive, error handler
is found. If no inactive, enabled error handler is found, the error is fatal
at the point at which it actually occurred. Each time the error handler passes
control back to a calling procedure, that procedure becomes the current procedure.
Once an error is handled by an error handler in any procedure, execution resumes
in the current procedure at the point designated by the Resume statement.
Note: An error-handling routine is not a Sub procedure or Function procedure. It is a section of code marked by a line label or line number.
To determine the cause of an error error-handling routines use the value returned
by the Err() function. Error-handling routines should check or save the
values returned by Err() and Erl() before a new error may occur,
or prior to calling a procedure, which may cause an error. These values describe
respectively the number of the last error and the line number in the source
module. Text of the error message corresponding to the error code can be obtained
by using the Error$() function.
On Error Resume Next causes execution to continue with the statement
immediately following the statement that caused the run-time error, or with
the statement immediately following the most recent call out of the procedure
containing the On Error Resume Next statement. This statement allows
execution to continue despite a run-time error. You can place the error-handling
routine where the error would occur, rather than transferring control to another
location within the procedure. An On Error Resume Next statement becomes
inactive when another procedure is called, so you should execute an On Error
Resume Next statement in each called routine if you want inline error handling
within that routine.
Note: The On Error Resume Next construction may be preferable to On Error GoTo when handling errors generated during access to other objects.
On Error GoTo 0 disables error handling in the current procedure. It
doesn't specify line 0 as the start of the error-handling code, even if the
procedure contains a line numbered 0. Without an On Error GoTo 0 statement,
an error handler is automatically disabled when a procedure is exited.
To prevent error-handling code from running when no error has occurred, place
an Exit Sub or Exit Function statement immediately before the
error-handling routine, as in the following fragment:
Sub foo()
On Error GoTo ErrorHandler
. . .
Exit Sub
ErrorHandler:
. . .
Resume Next
End Sub
Here, the error-handling code follows the Exit Sub statement and precedes
the End Sub statement to separate it from the procedure flow. Error-handling
code can be placed anywhere in a procedure.
Note: System errors during calls to dynamic-link libraries (DLL) do not raise exceptions and cannot be trapped with ConceptDraw Basic error trapping. When calling DLL functions, you should check each return value for success or failure (according to the API specifications).
Sub OnErrorStatementDemo() On Error GoTo ErrorHandler ' Enable error-handling routine. Open "TESTFILE" For Output As #1 ' Open file for output. Kill "TESTFILE" ' Attempt to delete open file. On Error Goto 0 ' Turn off error trapping. On Error Resume Next ' Defer error trapping. Dim d As Double d = 10 / sin(0) ' "Division by zero" error and resume next statement d = 20 / cos(0) Trace d Exit Sub ' Exit to avoid handler. ErrorHandler: ' Error-handling routine. Select Case Err() ' Evaluate error number. Case 55, 75 ' "File already open" or "Path/File access error" error. Trace """File already open"" or ""Path/File access error"" error" Close #1 ' Close open file. Case Else ' Handle other situations here... Resume Next End Select Resume ' Resume execution at same line that caused the error. End Sub |
See Also |
End Statement, Err Function, Erl Function, Exit Function Statement, Exit Sub Statement, Resume Statement , Trappable errors |