If you haven't set up a mechanism to handle errors, run-time errors in your code can lead to a variety of unfortunate outcomes. Some errors may abruptly halt the execution of your code, leaving the user no way to recover data, correct the source of the error, or resume execution. Other errors might allow execution to continue but will cause your code to act unpredictably.
For example, the following FileExists function, which contains no error-handling code, returns True if the specified file exists or False if it doesn't exist.
Function FileExists(filename) FileExists = (Dir(filename) <> "") End Function
The Dir function returns the first file that matches the specified filename; it returns a zero-length string if no matching file is found. Therefore, the code Dir(filename) <> "" returns True if Dir returns a filename or False if Dir returns a zero-length string.
Although this code appears to cover either of the possible outcomes of the Dir call, if the drive letter specified in the argument doesn't refer to a valid drive, error 68 ("Device unavailable") occurs. Also, if the specified drive is a floppy disk drive, the code will work correctly only if there is a disk in the drive and the drive door is closed. If not, Visual Basic generates error 71 ("Disk not ready") and halts execution of your code.
To avoid this situation, you can use the error-handling features in Visual Basic to intercept errors and take corrective action. (Intercepting an error is also known as trapping it.) For example, the disk-drive errors described in the preceding paragraph could be handled by the following code.
Function FileExists (filename) On Error GoTo CheckError ' Turn on error trapping so error handler
' responds if any error is detected. FileExists = (Dir(filename) <> "") Exit Function ' Avoid executing error handler
' if no error occurs. CheckError: ' Branch here if error occurs.
' Define constants to represent Visual Basic error code. Const ERR_DISKNOTREADY = 71, ERR_DEVICEUNAVAILABLE = 68 FileExists = False If Err = ERR_DISKNOTREADY Then Msg = "Put a floppy disk in the drive and close the drive door." ' Display message box with an exclamation mark icon and with OK
' and Cancel buttons. If MsgBox(Msg, vbExclamation + vbOKCancel) = vbOK Then Resume Else Resume Next End If ElseIf Err = ERR_DEVICEUNAVAILABLE Then Msg = "This drive or path doesn't exist: " & filename MsgBox Msg, vbExclamation Resume Next Else Msg = "Unexpected error #" & Str(Err) & ": " & Error(Err) ' Display message box with Stop sign icon and OK button. MsgBox Msg, vbCritical End End If End Function
In the preceding code, the Err function returns the error number associated with the run-time error that occurred. When Visual Basic generates error 71, the FileExists function displays a message that tells the user to insert a floppy disk in the floppy disk drive and allows the user to click either OK or Cancel to dismiss the message box.
If the user clicks OK, the Resume statement returns program control to the statement at which the error occurred — in this case, the line containing the Dir function — and attempts to reexecute that statement. This statement succeeds if the user has corrected the problem; otherwise, the program returns to the error-handling code.
If the user clicks Cancel, the Resume Next statement returns program control to the statement following the one at which the error occurred — in this case, the Exit Function statement.
If error 68 occurs, Visual Basic displays a message describing the problem. The Resume Next statement then causes the function to continue execution at the statement following the one at which the error occurred.
If an unanticipated error occurs, Visual Basic displays an alternative message and halts the code at the End statement.
Note
In the preceding example, the error numbers that the Err function returns are compared with constants that you've defined. For a list of the error numbers, see "Trappable errors" in Help.
The constants that begin with the letters "vb" are built-in constants that pertain to dialog boxes displayed by the MsgBox function. For more information about the MsgBox function, see "MsgBox" in Help.