On Error Statement

Description

Enables an error-handling routine and specifies the location of the routine within a procedure; can also be used to disable an error-handling routine.

Syntax

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, which is any line label or line number. Thereafter, if a run-time error occurs, control branches to line. The specified line must be in the same procedure as the On Error statement. If it isn't, a compile-time error occurs.

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. In other words, execution continues.

On Error GoTo 0

Disables any enabled error handler in the current procedure.


Remarks

If you don't use an On Error statement, any run-time error that occurs is fatal; that is, an error message is generated and execution stops.

If an error occurs while an error handler is active (between the occurrence of the error and a Resume, Exit Sub, Exit Function, or Exit Property statement), the current procedure's error handler can't handle the error. If the calling procedure has an enabled error handler, control is returned to the calling procedure and its error handler is activated to handle the error. If the calling procedure's error handler is also active, control is passed back through any previous calling procedures until an inactive error handler is found. If no inactive 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 the 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.

Notice that an error-handling routine is not a Sub or Function procedure. It is a block of code marked by a line label or line number.

Error-handling routines 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 a procedure that could cause an error is called. The value in Err reflects only the most recent error. You can use the Error function to return the error message associated with any given run-time error number returned by Err.

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 error-handling routine. This allows execution to continue despite a run-time error. You can then build the error-handling routine in line with the procedure rather than transferring control to another location within the procedure.

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, Exit Function, or Exit Property statement, as appropriate, immediately ahead of the error-handling routine, as in the following example:


Sub InitializeMatrix(Var1, Var2, Var3, Var4)
    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 normal procedure flow. This is only one possible solution; error-handling code can be placed anywhere in a procedure.

See Also

Err Function, Error Function, Resume Statement.

Example

This example uses the On Error GoTo statement to specify the location of an error-handling routine within a procedure. Attempting to delete an open file generates error number 55. The error is handled in the error-handling routine and control is then returned to the statement that caused it.


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.
    Exit Sub    ' Exit Sub before error handler.
ErrorHandler:    ' Error-handling routine.
    Select Case Err    ' Evaluate Error Number.
        Case 55    ' "File already open" error.
            Close #1    ' Close open file.
        Case Else
            ' Handle other situations here... 
    End Select
    Resume    ' Resume execution at same line
                ' that caused the error.
End Sub