Section 11.1. Error Detection and Error Handling


11.1. Error Detection and Error Handling

Dealing with errors in your code generally occurs in two steps: error detecting and error handling. In most cases, you deal with the consequences of an error immediately, merging these two steps into one seamless process. You can also break up the steps, recognizing an error immediately but dealing with it later on in your block of code, or even in a different part of your application. The code location where an error occurs is called the offending procedure.

There are two types of errors that can occur in a running program: runtime errors and logic errors . A runtime error occurs when the code attempts to perform an operation that is impossible to perform, such as opening a file that does not exist or dividing by zero. Visual Basic automatically takes care of error detection of runtime errors because it has no other choice. Proper error handling of runtime errors is up to the programmer. Without programmer action, Visual Basic itself handles the error by presenting an error message to the user and then terminating the application, which is, at the very least, a nuisance for the user.

A logic error is the production of an unexpected or incorrect result. Consider a function that returns the IQ for an individual based on a set of IQ test scores. A result of 100 is entirely normal and expected. If an individual is very smart, you might expect an IQ in the range of 120 or more. But if the function returns an IQ of -350, that is a logic error, either due to bad code, bad data, or both.

Visual Basic does not provide error detection for logic errorsno computer language doesbecause to Visual Basic, no error has occurred. However, an unhandled logic error may subsequently result in a runtime error, which Visual Basic will certainly recognize. For instance, code that is intended to retrieve a positive nonzero integer may instead retrieve zero. This is a logic error. If that integer is later used as a denominator in some other part of the application, the seemingly small logic error will become a large runtime problem.

The programmer must anticipate logic errors and provide both error detection and error handling. From this perspective, logic errors are far more serious and much more difficult to deal with than runtime errors. After all, a runtime error won't be completely overlookedat least Visual Basic will do something about it.

The problem with an overlooked logic error is that it may give the user specious feedback. This is, no doubt, the most insidious behavior a program can produce. In the best-case scenario, a logic error will generate a runtime error at some later time, but tracking down the original logic error source of that runtime error may be a daunting task.




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net