The Three Different Types of Errors

   


Our alien was able to take off with the spaceship in this case. But what if he was unable to leave due to an unknown error. Well, with a laser gun pointed at our heads by an enraged alien, we would have to consider several possibilities quickly. By knowing the three different types of errors described below, we would certainly be able to locate the problem faster.

  • Syntax error Refers to a grammatical mistake in the C# source code. There are very strict rules for how to write a C# program. These rules form the grammar for C#. This grammar is equivalent to the grammar of the English language, but much more concise. The grammar rules for a programming language are called the syntax of the language.

    It is easy to make a syntax error. Even omitting a semicolon when expected will cause a syntax error.

    During its compilation process, the C# compiler will look for syntax errors and print error messages on the screen. It will even try to tell you what caused the error and where it thinks the error is located. However, this information is not always correct because the compiler only makes informed guesses.

    A syntax error reported by the spaceship's computer would have been due to a mistake made by the alien when he converted the pseudocode to C# code; in which case, he would owe us an apology for groundless threats.

  • Logic error Had there been a mistake in the underlying logic presented with the pseudocode, we would refer to this as a logic error. Given that the alien converted all instructions exactly as shown in the pseudocode into a valid C# program without any syntax errors, he would, accordingly and unknowingly, have brought the logic error along into the C# program.

    For example, if line 3.c would mistakenly read "Add 2 to the ListSize," we would get a ListSize twice as big as the correct size and, as a result, end up with a calculated average only half of the correct average. This would most likely be a large enough error to prevent the alien's spaceship from taking off. In this case, we would apologize and plead for mercy.

  • Runtime error If the computer detects and reports an error message during the execution of the program, we refer to this as a runtime error. In this case, the computer will stop executing the program, and report the type of error along with its approximate location. However, the system might misinterpret the problem in many cases and lead you in the wrong direction.

    A runtime error could be caused by either a logic error or an incorrect conversion into the C# language from pseudocode. Even for our alien friend with his abilities to convert pseudocode into C# code, it would be possible to introduce bugs resulting in runtime errors. For example, the alien might not realize how large the Sum number can get during a specific calculation and, through a declaration in C#, allocate too little main memory for the Sum number. When the Sum number gets larger than this allocated main memory, a runtime error might be reported.

    Hopefully, we would be able to explain this possibility to the alien and save our lives.

Unfortunately, it looks as if we have, in fact, made an error in the pseudocode that could easily cause a runtime error. Consider a scenario where the list of numbers is empty. We then end up with Sum and ListSize being zero, dividing zero by zero in step 4. This is considered an invalid operation by the computer, and you will receive the following message, "The application has generated an exception that could not be handled." Fortunately, all the calculations in the spaceship must have involved lists of one or more numbers for it to take off. But we might not be as lucky next time.

Note

graphics/common.gif

The test performed by the alien of the "average calculating" algorithm by using the list { 1,2,3,4,5} seemed to ascertain the correctness of the algorithm. However, we have just seen one instance proving the algorithm to be faulty. This is a good example of how delicate software testing is.

It is impossible to prove that a program is correct simply by testing it. On the other hand, a program can be proven to be incorrect if errors are detected.



   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

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