Error Handling

All programs contain errors (often called bugs). Syntax errors occur when the programmer violates the rules of the language (for example, misspelled keywords, missing components of a code structure, or improper declaration of a variable), preventing the program from compiling. Syntax errors are relatively easy to fix because the VBA debugger sends you right to the source of the problem. Logic errors occur when the code contains errors that result in improper program behavior (for example, an infinite loop or wrong variable initialization). Logic errors do not prevent the program from compiling and executing; therefore, logic errors can be difficult to find. With proper debugging, however, the number of errors in a program can be significantly reduced.

Besides syntax and logic errors, it is possible that a program's code may generate a runtime error as a result of invalid input. Examples might include a divide by zero error (as seen in Chapter 6) or a file not found error. Programmers must anticipate errors such as these because if they are left unchecked, these errors will cause the program to crash. Furthermore, errors of this type cannot be fixed by altering the logic of the program. In situations such as these, the program requires additional error handling code and procedures. Error handling code should be included whenever the program interacts with the user or other components of the computer. Validation procedures are examples of error handling procedures; I have already discussed adding them to your code (see Chapter 4). This section will focus on special statements and objects available in VBA for handling anticipated errors.

Biorhythm Awareness

Biorhythm Awareness

Who else wants to take advantage of biorhythm awareness to avoid premature death, escape life threatening diseases, eliminate most of your life altering mistakes and banish catastrophic events from your life.

Get My Free Ebook


Post a comment