Page

9.2.2- Logical Errors

  by NT Community Manager.
Last Updated  by NT Community Manager.  

PublicCategorized as 09. Error Handling.

Not tagged.
<< 9.2.1- Syntax ErrorsChapter99.2.3- ASP Errors >>

Logical Errors

I've seen these kinds of errors called everything under the sun from "algorithmic errors" to "why the heck isn't my program working, that can't possibly be the right answer ". Most often, you've checked your code, cleared up any typos and obvious coding errors, and yet the program's not returning data or is returning data that can't possibly be correct. That's usually because you've made a mistake in the programming logic. Let's consider some common types of logical error:

 

  • Division By Zero Errors
  • Type Mismatch Errors
  • No Error message, but the output is wrong

 

Division by zero is where, for whatever reason, you end up dividing a number by zero. It's usually not an intentional mistake: you divide a variable by a positive number, but for some reason the variable is empty and so you inadvertently end up dividing your number by zero. Of course this is a mathematical impossibility and the computer will generate an error whenever this problem arises. However the problem might not always be apparent. Consider a form where you ask the user to enter a number of items for purchase, and you wish to give him/her a discount and calculate requisite taxation. The user accidentally forgets to enter this number in the box and submits it to your server. Your program multiplies the number of items by the price and ends up with zero. This result could then be used as basis for division, later in the calculation, and we end up with a division by zero error. However if the user had supplied a number other than zero then the program would have worked correctly.

 

Type mismatch errors occur, for example, when variants of one subtype (such as integers) are used to store the results of a calculation and then are added to information of another incompatible subtype (such as text). As variants are "variables for all seasons", they're normally very durable, so you can assign it a number one moment and text the next:

 

<%

varint = 1

Response.Write varInt

varint = "Hello"

Response.Write varInt

%>

 

The implicit type conversion would make sure, that the first value is handled as a number and the second is handled as text. Thus, you'd get the response 1 followed by Hello. However, if you tried to add text to a variant storing a numerical value, then you'd generate an error:

 

<%

varInt = 1

Response.Write varInt

varInt = varInt + "Hello"

Response.Write varInt

%>

Another common occurrence of this type of error is where you attempt but fail to create an object and then try and use it as an object, while ASP still thinks you're using the variable designed to hold an instance of an object, as a variable. Later in this chapter we will look at a method that can help to reduce the frequency at which this type of error occurs.

 

I'd hazard that one of the most common types of logical error is where the programmer makes a mistake in his/her assumptions, and codes that error into the program. Consider the following pearl – whether it's unverifiable truth or merely urban myth is open to speculation, but it could have happened. In the 1950s/60s, on the U.S. nuclear defense system, a programmer calculated a number incorrectly – he or she managed to shift the decimal point one place to the left. Unfortunately, the number in question corresponded to a flight trajectory angle in the algorithm that detected whether a nuclear missile attack had been launched. Once in operation the program detected an object corresponding to its calculations, and displayed that it was 99.9% certain that it was an incoming missile. It turned out that the program had been triggered by the rising moon!

 

In the business world, such mistakes are probably less dramatic, but consider the following program to calculate the royalties that a best-selling novel writer might receive from his publishers:

 

<%

intSales = 10000 'Sales for One Quarter

intRoyaltyRate = 0.1 '1% Royalty Rate

intPrice = 40 'Book Price

intRoyaltyReceived = intSales * intRoyaltyRate * intPrice

%>

 

All looks very plausible until you realize the mistake. The author is only meant to be getting 1% royalties, not 10%. The correct line should read:

 

intRoyaltyRate = 0.01 '1% Royalty Rate

 

If you calculate the difference between the two totals, you'll find that the first is 40,000 dollars, while the corrected one is 4,000 dollars. This simple accounting mistake could be costing the publisher 36,000 dollars, if the author forgets to mention it.

 

Of course there are plenty of other types of logical errors, but hopefully these examples have given you an idea of the kind of thing you should be looking out for when you're checking and testing your code.

<< 9.2.1- Syntax ErrorsChapter99.2.3- ASP Errors >>

Copyright © 2003 by Wiley Publishing, Inc.

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy |