VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > Advanced VB6 tutorial > Chapter 11

Handling Errors in Code - Using the On Error Statement

The On Error statement in Visual Basic identifies how errors will be handled for a particular routine. It can be used to turn on and turn off error handling for a procedure and, in some cases, sub-routines and functions called from the procedure in which it is coded (see "Using the Error-Handling Hierarchy"). An On Error statement instructs Visual Basic on what should be done if a runtime error occurs.

It is good practice to place error handling in every procedure. Generally it is especially important in any routine prone to errors. These include routines that process database information, routines that read from and write to files, and procedures that perform calculations. If an application contains code that relies on some outside events—such as a network connection being available or a disk being ready in a drive—there are always situations that are beyond the control of the developer. For these instances good error-handling routines are very important.

Different routines require different types of error handling. The syntax of the On Error statement can be coded several ways, depending on each situation.

Goto <line>

The first way to code the On Error statement is as follows:

On Error GoTo Main_Error

where Main_Error is a line label in a procedure. This is the most common use of the On Error statement and gives the developer the most control over error handling. A procedure using this format would look something like this:

Private Sub Main()
   On Error GoTo Main_Error
   ' ... some processing ...
   Exit Sub
   ' error handling code
End Sub

As in the preceding procedure, it is generally best to put the On Error statement as the first executable statement in a procedure so that any other lines of code will fall under the control of the On Error statement. In this example when an error occurs anywhere after the On Error statement, execution will continue in the errorhandling code.

Error-handling code can contain any Visual Basic statements that can be coded elsewhere. It is important, however, to keep errorhandling code simple to prevent additional errors from occurring. If a runtime error occurs in the error-handling code, then the new error will not be handled in the error handler. Instead, the error will be passed up the call stack until either an error handler is encountered, or until the error becomes a runtime error.

Resume Next

The second way of coding an On Error statement is with a Resume Next clause. With a Resume Next clause, the On Error would look like this:

On Error Resume Next

Resume Next tells Visual Basic that when a runtime error occurs, ignore the statement that caused the error and continue execution with the next statement.

GoTo 0

The last way to code the On Error statement is with the GoTo 0 clause, as in the following:

On Error GoTo 0

This is different from the other On Error statements in that it disables rather than enables error handling for the current routine. Even if there is a line labeled "0", error handling will be disabled.


<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved