Sign In
Link to us
Home
1. Introduction
2. Introducing VB
3. Projects
4. Containers
5. Controls
6. Code
7. Forms
8. Debugging Code
9. Error Handling
 9.1 Intercepting Errors
 9.2 Determining Errors

10. Objects
11. User Controls
12. Data Projects
13. API
14. Distribution

Other Resources


Affiliated Links:
www.freefunfings.com


9. Error Handling

Visual Basic is capable of "trapping" some of the error that it generates. A "trappable" error is one that can be detected and handled without crashing the application. For example, if your application tries to open a text file that doesn't exist, an error will occur. You may want to intercept this error and prompt the user to select an alternative file to use.

Information about the current error is held in the Error object. This special object is always available in Visual Basic.

9.1 Intercepting Errors

When Visual Basic encounters an error under normal conditions it stops execution of the code. If you are running the application under the Visual Basic design environment, you will be prompted with the error and asked if you want to debug the code that caused it. If you have made the application (e.g. into a .EXE file) the error will cause the application to terminate.

If Visual Basic detects the occurrence of an error, there are 3 ways that it can be handled:

On Error Resume Next
On Error Goto line
On Error Goto 0
Typing the first line will cause the code to continue from the next line after the error. This must be used carefully, as the line that caused the error will effectively be ignored.

If you want to handle the error within the code, use the "On Error Goto line" option. This will cause the code to jump to a labelled line within the procedure and, if necessary, jump back to retry the code that gave the error:

Private Sub MySub()
    On Error Goto MyLabel
    strA = strB & strC
    strZ = GetResult(strA)
    Exit Sub                 ' Exit the Sub here so that Error routine is not executed if no error occurs
    MyLabel:
    MsgBox "Error!"          ' If an error occurs above, code jumps to here.
    Resume                   ' "Resume" will retry th line that cause the error.
                             ' "Resume Next" will return to the line AFTER the one that caused the error
                             ' "Resume label" will return to the specified line label
End Sub
Finally, if you use the "On Error Goto 0" option, the error handling will be cancelled. This means that you can handle errors between specific sections of code by enabling and disabling the error hander.

9.2 Determining Errors

Once you have intercepted the error, you may want to know what type of error has occurred, so that you can report it to the user or log it in a file somewhere. This is achieved by referencing the Err object. This object contains useful information about the current error, which you can use to handle you code's reaction to it.

9.2.1 Error Number
The "Error Number" is a unique identification number for the type of error that has occurred. This is the value returned from the Number property of the Err object.

If Err.Number = 5 Then
    AddLog("ERROR 5")
End If
9.2.2 Error Description
The "Error Description" provides an English language description for the type of error that has occurred. This is the value returned from the Description property of the Err object.

If Err.Number = 5 Then
    AddLog( Err.Description )
End If
<<Page 8  :   Top  :   Page 10>>
©vbtraining.co.uk