Saving & Executing Projects / Recognizing Error Types
Subtitles of the Movie
In this movie we will focus on error types. There are three basic types of errors that you will encounter in Visual Basic 2005: syntax errors, logic errors, and runtime errors. Syntax errors are when a rule of the language has been violated in some way. It could be something as simple as misspelling or misuse of punctuation. Syntax errors must be corrected in order for you to continue the programming process. Logic errors are more difficult to find because output is produced but the results are not correct. You must take care in creating sufficient test data to make sure your programming is producing the desired output. And finally, runtime errors, runtime errors occur only when the project is being executed. This type of error may involve for example when you're trying to access a file that is not present or it may be a memory type error. Now let's take a look at some examples of these types of errors in the Visual Basic environment. As you can see, I have a sample program that I will use to demonstrate these basic error types. The purpose of this program is to allow the user to enter two numbers and then to click the Calculate button. Then the result of the calculation should be displayed in the label at the bottom. The two numbers are to be added together, that is the sum of the numbers should be displayed. So before we introduce an error to this program let's run the program as is to ensure that it does indeed run correctly. Clicking the Start button allows the program to begin. The program is now in runtime and the user can interact with the program. Let's enter two numbers, I'll enter the number 6 and the number 8, and then click Calculate, and as you can see, the correct answer of 14 is displayed in the result label. That is, 6 plus 8 is 14. Let's run it one more time with a different set of data just to make sure the program is running correctly. I'll enter 12 and 7. Calculate, once again the correct answer in this case 19 is displayed. So now I'll stop the program and we'll introduce some errors. The only real place in this program that an error could take place would be associated with the calculate button, since that's the only place where coding is involved. So take a look at the code for the Calculate button, we simply double click Calculate at design time and then that will display the event procedure or the code window for Calculate button. I'll close the toolbox to allow us to have a good view of the code. As you can see it's only one line of code and it's very simple. This line of code is going to display in the answer and label 1 label the result of adding Number 1 text box and Number 2 text box. Now the creator of this program would have already named the controls for the text box Number 1 and Number 2 respectively. These are entirely made up by the programmer and they can be called anything as long as they follow the rules that we'll talk about later. The result is being displayed in the Answer label. Now if I were to change the name of Number 1 to simply Number, thereby deleting the number 1, as soon I do that you see immediately that name is underlined with a blue line. If I move the cursor over that blue line I'm given the error message ÒNumber text box is not declared.Ó This is an example of a syntax error. I am using a name of an object which Visual Basic does not recognize as being on the form. If I were to try to run this program by clicking the Start button, I get the message ÒThere were build errors, would you like to continue and run the last successful build?Ó Now if I were to respond yes here, the program would run the last executable program which was the one I ran previously, and it would run correctly but that's not this program, so I want to answer no, and again, I'm shown an error list and it shows me the same message, ÒNumber text box is not declared.Ó It also gives me a line number and a column number where this is occurring. So since we know this is going to create a problem, we'll go ahead and correct it by entering the next, the number 1 in the Number 1 text box. As you can see, as soon as I do that, the blue line goes away. Another quick example, if I were to delete the word Label from Answer Label, and simply call it Answer, you see immediately ÒAnswer is not declared,Ó and you see that down there a List Box that is now open. So again, you have to take care that you are spelling the names of objects exactly as they are spelled on the form, otherwise you're committing a syntax error. Another example of syntax error is the misuse of punctuation. You may have noticed that there is a period or a dot in between the name of the object and the property. If I were to for example use a comma instead of a period, as you can see the entire line is highlighted and actually a space has been inserted after the comma and that has created an error. So again, something as simple as punctuation can solve a problem. So I'll put it back in the correct form. Now remember the purpose of this program was to add two numbers together so now that I've made some changes and hopefully put them back correctly let's run the program and confirm that it is working correctly and then we'll introduce another type of error. So again I'll enter 4 plus 8, Calculate, 12. So again, the program is running correctly. Returning to our code window, I will now introduce a logic error, a logic error is when you are telling it to do the wrong thing but it does produce results. For example, I have a plus symbol which is obviously adding the two numbers together, if I were to change that to an asterisk which is the multiplication symbol in Visual Basic, that's a perfectly legitimate operation, however it's the wrong operation. But let's run the program and you'll see what happens. So once again, I will enter a number 4 and I'll enter the number 6 this time and Calculate, as you can see the answer is shown as 24 which obviously is not 4 plus 6, it is 4 times 6. So this is an example of a logic error. Logically, the program is working incorrectly, it does produce results but those results are not correct. For this reason it is important to test your programs very carefully. For example, if I were to enter 2 and 2 and then Calculate, I get the correct answer of 4, 2 plus 2 is 4, 2 times 2 is 4. This is not good test data, you want to choose data which is going to give you specific results for the specific calculations you are testing for. As we know, this program is not working correctly, 2 times 3 is 6 but 2 plus 3 obviously is not 6, so again, the root problem is back here in the code with the asterisk.
Tutorial Information
| Course: | Microsoft Visual Basic 2005 |
| Author: | Arthur Lee |
| SKU: | 33940 |
| ISBN: | 1-935320-10-6 |
| Release Date: | 2008-11-19 |
| Duration: | 7.5 hrs / 97 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 