On Error Statement

See AlsoZ458PZ              ExampleEN76LP>Low

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

Syntax

On ErrorGoTo line | Resume Next | GoTo 0 }

Remarks

If you don't use an On Error statement, any run-timeCYRM35 error that occurs is fatal; that is, Visual Basic generates an error message and stops program execution.

The On Error statement has these parts:

Part                     Description

 

GoTo line             Enables the error-handling routine that starts at line (a line labelGH72Z1 or a line number18F50XF).  Thereafter, if a run-time error occurs, program control branches to line.  The specified line must be in the same procedure as the On Error statement.  If it isn't, a compile-timeCK544P error occurs.

Resume Next       Specifies that when a run-time error occurs, control goes to the statement immediately following the statement in which the error has occurred. In other words, the code continues to execute.  You can use the Err function in subsequent lines of code to obtain the run-time error number.

GoTo 0                Disables any enabled error handler in the current procedure.

 

An error handler is enabled when it is referred to by an On Error GoTo line statement.  Once an error handler is enabled, any run-time error causes program control to jump to the enabled error-handling routine and makes the error handler active.  An error handler remains active from the time a run-time error has been trapped until a Resume, Exit Sub, or Exit Function statement is executed in the error handler.

If an error occurs while an error handler is active (between the occurrence of the error and the execution of a Resume, Exit Sub, or Exit Function statement), the current procedure's error handler cannot 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 also is 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, program execution resumes in the current procedure at the point designated by the Resume statement.

Notice that an error-handling routine is not a SubQDBVHN or FunctionK6LBMC 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 program execution to continue with the statement immediately following the statement that caused the run-time error.  This allows your program to continue despite a run-time error and then check for the cause of the error.  This also lets you 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 executing when no error has occurred, place an Exit Sub or Exit Function statement 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 partition it from the normal execution flow of the procedure.  This is by no means the only solution.  Error-handling code can be placed anywhere in a procedure.


See Also

Err, Erl Functions3XXD805

Error, Error$ Functions5AEF2L

Resume StatementB4FKXK

Trappable Errors4LGWYDD


On Error Statement Example

The example uses the On Error statement to set up error handling in a procedure.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.

 

Sub Form_Click ()

   Dim Drive, Msg                           ' Declare variables.

   On Error GoTo ErrorHandler               ' Set up error handler.

   Msg = "This demo attempts to open a file that does not exist on a "

   Msg = Msg & "drive that may not exist. When the operation fails, "

   Msg = Msg & "the ErrorHandler routine will display a message box "

   Msg = Msg & "that indicates what error occurred."

   MsgBox Msg                               ' Display opening message.

   Drive = Chr(Int((26) * Rnd + 1) + 64)    ' Make random drive letter.

   Open Drive & ":\TEST\X.DAT" For Input As #1 ' Try to open file.

   Close #1                                 ' Close the file.

   Exit Sub                                 ' Exit before entering

                                            ' error  handler.

ErrorHandler:                               ' Error handler line label.

   Select Case Err

      Case 53: Msg = "ERROR 53: That file doesn't exist."

      Case 68: Msg = "ERROR 68: Drive " & Drive & ": not available."

      Case 76: Msg = "ERROR 76: That path doesn't exist."

      Case Else: Msg = "ERROR " & Err & " occurred."

   End Select

   MsgBox Msg                               ' Display error message.

   Resume Next                              ' Resume procedure.

End Sub