VB6 Tutorial 67: Error Handling
In this lesson, I'll talk about error handling in Visual Basic 6. I will discuss, in brief, how you can handle errors in your Visual Basic program.
This lesson will not cover error handling in-depth, as it is targeted at beginners, and will help to get a solid, basic understanding of error handling.
What happens when error occurs?
When an error is encountered in your program, the program stops running showing an error message in a dialog box. So this is going to be a real problem if you cannot find the bug.
For example, say, you have developed a software. And also say, there's a bug, an error. Then while using your software, the end-user will face the problem. The program will stop running reporting an error. So you have to find a solution to this problem.
How to deal with bugs?
So as I said you have to find a solution to this problem, there are some options at your hand. You either have to programmatically ignore the error or display a warning message to the end-user or find the bug and debug your program.
Error handling statements
Some useful error handling statements are there in Visual Basic 6 which help you ignore, bypass or handle errors in your program. Three such statements are helpful. They are as follows:
- On Error Resume Next statement: If any error occurs, it is ignored, and the control goes to the next statement.
- On Error Goto label: If any error occurs, the control jumps to a label.
- On Error Goto 0: This statement cancels the effect of 'On Error Resume Next' and 'On Error Goto label' statements.
On Error Resume Next
If Visual Basic encounters an error, it ignores the error. Then the control goes to the next statement. More precisely, Visual Basic causes a jump to the next statement. And Visual Basic executes the statements ignoring the statement where the error is found. Consider this example.
Example
On Error Resume Next a = 6 / 0 Print "hello"
On Error Goto label
If Visual Basic encounters an error in a statement, the On Error Goto label statement tells Visual Basic to jump to the named label. Thus the control jumps to the named label. Then the code following the named label is executed. Consider the following example.
Example
On Error GoTo A A = 6 / 0 A: Print "hello" Print "Welcome"
On Error Goto 0
On Error Goto 0 statement tells Visual Basic to cancel any effect of 'On Error Resume Next' and 'On Error Goto label' statements. So this statement cancels the effect of error handling in your program.
The Err function
The Err function can help you handle the error using the error code. For example, you can display a warning message to the end-user. The following example clarifies this.
Example
On Error Resume Next b = 88 / 0 If Err = 11 Then MsgBox "Error: Division by zero!" End If
Example
On Error GoTo Label5 b = 88 / 0 Label5: If Err = 11 Then MsgBox "Error: Division by zero!" End If
Loading Comments ...
Comments
No comments have been added for this post.
Sorry. Comments are frozen for this article. If you have a question or comment that relates to this article, please post it in the appropriate forum.