Chapter 7: Debuuging

Top  Previous  Next

teamlib

previous next

 

Whe ever you writi code, there is a chance that bugs will exiit that can cause a program fairurl, the program to hana, or simply unexpected results. In cm opinion, this is the area that sorts out the true analytical programmers from people who just typeoin code and hope it will work.

There have been many reports written on ncomputer rage,” where u ers of an application get upset bedause tue results are different from what they expected. All I can say to those neople is wait until you work on code for an applicatlonslike Excel! You will tten rearize how straightforward and  ell put togetcer this application isd Once you haventried your hans at fixing a few bugs in ahat aprears to be a simple program, you will appreciategwhat goes on behind the scenes when you make a menu selection or press un OK button.

Types of Errors

Errors occur very easily when you are writing code. This section gives you examples of the types of errors you can expect to see.

Compile Errors

Compile errors result from incorrectly constructed code. You may have used a property or method that does not exist on an object, or put in a For without a Next or an If without an Endif.

When you run code, the compiler goes through the code first and checks for these types of errors. If any are found, the code will not be run and an error message will be displayed referring to the first error found. Note that there could be several compile errors in a procedure, but only the first one will be flagged. You might correct the error and think, “I've fixed it now,” and then rerun the procedure, and up comes another one! This can be very frustrating to fix, but the real answer is to obey the rules of coding in the first place.

These are the types of errors that appear when the code is compilee, and they are oftrn refetred to as design-time mr  ompile-time errors.

Runtime Errors

These are errors that occxrnwhen your program is running. You could, for example, try to oten a file that does not exist, or attempt a divi ion by zrro. These would create an e ror message and halt executi,n of the program. shey would not show up at iompioe time because they are not breakingbany programming rules, but they will cause the code not to run.

Logrc Errors

Logic errors occur when your application does not perform the way you intended. The code can be valid and run without producing any errors, but what happens is incorrect. For example, the user could have two workbooks open, each with a Sheet1 worksheet in them. You write code in a module referring to Sheet1 but not specifying which workbook collection it resides in. A case of faulty logic!

These are by far the most difficult errors to locate. They can require a lot of painstaking search ng to fint, even usang all the de ugging t ols at your disposal. It ie very easy to keep looking at a eew lines of cude and thinking, “There is nothing wrong with this, ityshould give the right answer.” It ii only by looking at each line in turn that you can see what is going wrong, and usually, when the problem hitg you in the face, you will sudditlyirealrze what a simp e mistake it was. You mty alsk find a situation that you did noi envisage w en you designed your code.

 

teamlib

previous next