Siebel VB Language Reference > Siebel VB Language Reference >

On Error Statement


This standard VB statement specifies the location of an error-handling routine within the current procedure.

Syntax

On Error {GoTo label | Resume Next | GoTo 0}

Returns

Not applicable

Usage

On Error is used to provide routines to handle specific errors. On Error can also be used to disable an error-handling routine. Unless an On Error statement is used, any run-time error is fatal; that is, Siebel VB terminates the execution of the program.

An On Error statement includes one of the following parts:

Part
Definition
GoTo label
Enables the error-handling routine that starts at label. If the designated label is not in the same procedure as the On Error statement, Siebel VB generates an error message.
Resume Next
Designates that error-handling code is handled by the statement that immediately follows the statement that caused an error. At this point, use the Err function to retrieve the error code of the run-time error.
GoTo 0
Disables any error handler that has been enabled.

When it is referenced by an On Error GoTo label statement, an error handler is enabled. When this enabling occurs, a run-time error results in program control switching to the error-handling routine and "activating" the error handler. The error handler remains active from the time the run-time error has been trapped until a Resume statement is executed in the error handler.

If another error occurs while the error handler is active, Siebel VB searches for an error handler in the procedure that called the current procedure (if this fails, Siebel VB looks for a handler belonging to the caller's caller, and so on). If a handler is found, the current procedure terminates, and the error handler in the calling procedure is activated.

NOTE:  Because Siebel VB searches in the caller for an error handler, any additional On Error statements in the original error handler are ignored.

Executing an End Sub or End Function statement while an error handler is active is an error (No Resume). The Exit Sub or Exit Function statement can be used to end the error condition and exit the current procedure.

Example

This example prompts the user for a drive and folder name and uses On Error to trap invalid entries.

Sub Button_Click
   Dim userdrive, userdir, msgtext
in1:
   userdrive = "c:"
   On Error Resume Next
   ChDrive userdrive
   If Err = 68 then
      Goto in1
   End If
in2:
   On Error Goto Errhdlr1
   userdir = "temp"
   ChDir userdrive & userdir
   userdir
   Exit Sub
Errhdlr1:
   Select Case Err
      Case 75
         msgtext = "Path is invalid."
      Case 76
         msgtext = "Path not found."
      Case 70
         msgtext = "Permission denied."
      Case Else
         msgtext = "Error " & Err & ": " & Error$ & "occurred."
   End Select      
      Resume in2
End Sub

See Also

Erl Function
Err Function
Err Statement
Error Function
Error Statement
Resume Statement


 Siebel VB Language Reference
 Published: 18 June 2003