Irrespective of, the program is how much carefully developed and debugged, never it is impossible to eliminate possibility of origin of errors completely. Therefore at writing of steady programs it is necessary to process origin of unrecoverable errors. The command which is applied to their interception in specific procedure, looks as follows:
On Error GoTo...
Where three points mean a label (row number) defining the beginning of a fragment of handling of an error. The given fragment should be allocated within the limits of the same procedure. It is impossible to pass by means of command On Error GoTo to other procedure. On the other hand, fragments for interception of errors are often used in other procedures or functions.
The label for the operator of interception of an error represents any identifier with the last character in the form of a colon, satisfying to the agreement about именованиях variables. The label designates the beginning of a fragment of the program, as shown more low:
ErrorTrap:
' the program code follows further
As it is necessary to eliminate possibility of "embarkation" from the program in processing procedure of the errors, quite good idea is usage of operator Exit in line, directly ahead of a fragment label on interception of errors.
Command On Error GoTo can be in any place of processing procedure of event, function or procedure Sub. Normally in the same place the program code for error handling is allocated also. A unique exception is that cases when procedure is caused within the limits of other procedure. Then Visual Basic at first checks fragment presence on error handling at first in current procedure, and then in "higher".
After the programmer starts to use error handling by means of On Error GoTo, in its applications errors in runtime disappear. (But it does not protect from errors in an operating system; in Windows 95 they are a bit less, than in Windows 3.) Anyway command On Error GoTo should transfer control on a section of the code which identifies a problem and, probably, solves it.
If the error can be corrected, operator Resume is used for reset to a place in the program where it happened. However it is impossible to rectify an error, without knowing about the reason which has caused it. It can be made, using function Err and object Err. It represents the whole value which can be appropriated a variable. For example, if to write
ErrorNumber = Err. Number
That variable value ErrorNumber will be error number.
Visual Basic can identify more than 80 errors in runtime. (The information on it it is necessary to search in the electronic documentation in section "trappable errors"). It is more low resulted two examples.
|
|
|
|
|
Error of input of an output (for example, press attempt at выключеном the printer). |
|
|
The device is inaccessible (the device does not exist or is inaccessible at present). |
The method of usage of the given information is very simple. We assume, event processing procedure uses the printer. Somewhere in procedure in front of the operator of the press it is necessary to allocate a line
On Error GoTo PrinterCheck
Now in front of operator End Sub it is necessary to add the following code:
Exit sub
PrinterCheck:
ErrorNumber = Err. Number
Beep
Select Case ErrorNumber
Case 25
MsgBox "Your printer may be off-line."
Case 27
MsgBox "Is there a printer available?" Case Else
M $ = "Please tell the operator (= program author?) that"
M $ = M $ AND vbCrLf ' = Chr $ (10) + Chr $ (13) New Line
M $ = M $ AND "error number" AND ErrorNumber AND "occurred."
MsgBox M $
End
End Select
M $ = "If the error has been corrected click on OK."
M $ = M $ AND vbCrLf
M $ = M $ AND "Otherwise click on Cancel."
Continue = MsgBox (M $, vbOKCancel)
If Continue = vbOK Then Resume Else End
The idea of the given error handler is very simple, and SELECT statement Case approaches in this case ideally. In each separate case the program checks type of an error and offers possible recommendations about its correction. If reach the operator Case Else on the screen there is error number. Anyway after a cycle on error handling possibility of a choice by means of a data panel with two buttons is offered to the user. Probably, it is required to write the general procedure for error analysis. Then the fragment on error handling in procedure will transfer control above. After that such procedure can be used for interception and error handling in many various projects.
Interception of errors is not panacea. For example, it is difficult to make something if there is a failure of a hard disk or in the printer there is a paper.
The operator in whom there was an error allows to pass a variant of command Resume. If to use
Resume Next
Visual Basic the operator which is immediately after the leaking starts to process. It is possible even to use
On Error Resume Next
For the automatic pass of any program code causing an error. (It is resulted not casually though in the given chapter it is not applied.)
Besides, it is possible to restart operation of the program from any code line in which the label specifies. The following operator is for this purpose used:
Resume Label
Normally in Visual Basic labels are applied only to a designation of fragments on error handling. Despite it, for compatibility with prior versions BASIC imperative go-to statement GoTo is entered, but the need in it arises very rarely.
Commands Resume and Resume Next behave differently at reset back in that case when the error happened in other procedure. We recall that the such takes place, when one procedure is caused from another and in it there is a fragment on error handling: In both cases Visual Basic does not pass to initial procedure. In case of operator Resume there is a repeated procedure call. In case of Resume Next Visual Basic starts to fulfill the program from the first (after a procedure call) the operator. That is there is no reset to initial procedure.
Let's assume that the chain of the interconnected procedures was extended: Процедура1 causes Protseduru2, and that, in turn, FunktsijuZ. We admit that the error happens at the moment of performance of FunktsiiZ, but the error handler is only at Protsedury1. If at the given fragment there is operator Resume Visual Basic passes to the operator causing Protseduru2. As such approach actually very bulky it is better to rely always only on the error handlers which are in current procedure. If one procedure causes another at it it is necessary to disconnect a fragment on error handling.
There is one more function for error handling, Erl (Error Line). If the programmer despaired to find a line in which there is an error, a Visual Basic does not stop on the given line, it is necessary to do the following:
Error (error status code number)
Which forces Visual Basic to work as if there was an error to specified number. It facilitates process of debugging of interception of errors.
If the programmer is assured that the program more does not need an error handler, it can disconnect it by means of the operator
On Error GoTo 0
(Though value 0 in general not so also is necessary). Similarly it is possible to change the link to other error handler, using other operator On Error GoTo. It is necessary to be convinced that between error handlers there is operator Exit. Visual Basic the last operator On Error GoTo uses to solve, where to move further.
More in detail about object Err
To provide more centralized error handling, when necessary, Visual Basic uses the special object Err which properties can be analyzed after error origin. Earlier we saw, how property Number (Err works. Number), producing error number.
After operator Resume or On Error is fulfilled, all properties of object Err are dropped in 0 or reduced by the line "". Properties of the given object are initialized and after an output from function or procedure. Besides, for this purpose it is possible to use the operator
Err. Clear
If it is necessary to generate an error for the test purposes, it is possible to use method Raise of object Err. Its syntax following:
Err. Raise (number)
If it is necessary to deduce the description of a current error, the operator is used
Err. Description