Handling and Logging Errors in VB Programs
Handling and Logging Errors in VB Programs
VB does not handle the error automatically. It simply notifies the program of the error. If your program has been setup to handle errors, you can retry an operation, allow the user to correct a value or exit the programs. If you do not have an error handler in your program, your program terminates with error messages. There are several processes that you need to understand in handling errors. 1. Have a good understanding of the Err object. 2. Be able to create an error handling routine using the On Error statements. 3. Know how to use proper error trapping options. Understanding errors:- As you are developing programs, you can encounter three classes of errors. Syntax Errors: - You have incorrectly entered a line of code. Logic Errors: - Your program does not produce correct results but runs ok. Runtime Errors: - A condition in your program causes errors. Runtime error occurs when something unexpected happens in your program or a statement in your program can not be correctly resolved. Some examples of runtime errors are 1. 2. 3. 4. Trying to open a file that does not exist. Trying to assign a string to a numeric variable. Dividing a number by zero. Saving a database record that is locked by another user.
Some of these errors can be avoided but many times the causes of errors are beyond your control. When there are no ways to avoid these errors, what you can do is to take action when the error occurs.
Avoiding Errors: - You can trap a division by zero error but it is easier to avoid the error in the first place. If y=0 then Msgbox Enter a non zero value for Y Z=0 Exit Sub Else Z=x/y End If Working with Err object: The Err object is the centerpiece of Visual Basic error notification and handling capacities. Using the Err object you can determine which error occurred, get a description of the error and find out where the error occurred. You can use Err object to even raise your own errors from code. The Err object provides information to the program and takes action through the properties and methods of objects. The Err object has no events. When an error occurs in the program, it populates the properties of the err object to provide the information about the error to you and your program. The most important property of Err object is Number property. This property tells you, which of the VBs hundreds of possible errors has occurred. This number is used in error handling routines to determine which action to take. The counterpart of the Number property is the Description property of the Err object. It provides with the brief description of the error that occurred.
Defining Error Handler statements: 1. On error statements: - To tell VB where to go when an error occurs. 2. Resume statements: - To tell the program what to do after the error is handled. On Error GoTo line: - tells VB to branch to the line no. or line label specified in the command whenever an error occurs. This line no. or label corresponds to the beginning of the error handling routines in the procedure Eg: Private Sub cmdClick_Click() On Error GoTo ErrorHandler: x=val(Text1) y=val(Text1) z=x/y Text3=z ErrorHandler: Msgbox Err.Description End Sub
On Error Resume Next: - tells VB to move to the next line in the code if an error occurs. If the error is in the current procedure, the next line of the code is executed. If the error occurs in the sub-procedure called from the current procedure execution continues from the next statement after procedure call.
On Error GoTo 0: - tells VB to disable the error handlers in the current procedure. If an error occurs, the error is passed up to determine if an error handler can be found. If there are no error handlers a VB runtime error is generated and your program terminates.
Resume statements: Resume : - tells VB to retry the statement that caused the error. If the statement is a Sub procedure call, the call is repeated and the sub procedure is run from the beginning. Resume Next: - tells VB to continue execution on the line following the one where the error occurred. If the error occurred in a sub procedure, the line following the procedure call is the next one executed. Resume Line : -tells VB to continue execution at the line number or line label specified in the resume statement. The label must be in the same procedure as the resume line statement. You use the resume line statement to branch around the statement that would cause additional errors after the first one has occurred.