Tuesday, July 31, 2007

Kinds of Program Errors

Ever wonder what are the common kinds of program errors and what do they mean?
Here's an explanation about them..

The compiler will catch certain kinds of mistakes and will write out an error message when it finds a mistake. It will detect what are called syntax errors since they are, by and large, violation of the syntax or simply the grammar of the programming language, like omitting the semicolon in C or C++.

If the compiler discovers that your program contains a syntax error, it will tell you where the error is likely to be and what kind of error it is likely to be. If the compiler says your program contains a syntax error, you can be confident that it does. However, the compiler may be incorrect about either the location or the nature of the error. It does of determining the location of an error, to within a line or two, than it does of determining the source of the error. This is because the compiler is guessing at what you meant to write down and can easily guess wrong. After all, the compiler cannot read your mind. Error messages subsequent to the first one have a higher likelihood of being incorrect with respect to either the location or the nature of the error. Again, this is because the compiler must guess your meaning. If the compiler's first guess was incorrect, this will affect its analysis of future mistakes, since the analysis will be based on a false assumption.

If your program contains something that is a direct violation of the syntax rules for your programming language, the compiler will give you an error message. However, sometimes the compiler will give you only a warning message, which indicates that you have done something that is not, technically speaking, a violation of the programming language syntax rules, but that is unusual enough to indicate a likely mistake. When you get a warning message, the compiler is saying, "Are you sure you mean this?" At this stage of your development, you should treat every warning as if it were an error until your instructor approves ignoring the warning.

There are certain kinds of errors that the computer system can detect only when a program is run. Appropriately enough, these are called run-time errors. Most computer systems will detect certain run-time errors and output an appropriate error message. Many run-time errors have to do with numeric calculations. For example, if the computer attempts to divide a number by zero, that is normally a run-time error.

If the compiler approved of your program and the program ran once with no run-time error messages, this does not guarantee that your program is correct. Remember, the compiler will only tell you if you wrote a syntactically correct program. It will not tell you whether the program does what you want it to do. Mistakes in the underlying algorithm or in translating the algorithm into the C++ language are called logic errors. Example of this is, instead of having the symbol + for your addition of two variables, you use * instead, which is for multiplication. This will simply yield an erroneous result. If the compiler approves of your program and there are no run-time errors, but the program does not perform properly, then undoubtedly your program contains a logic error. Logic errors are the hardest kind to diagnose, because the computer gives you no error messages to help find the error. It cannot reasonably be expected to give any error messages. For all the computer knows, you may have meant what you wrote.


TIP:

If you want to test your program for Logic Errors, you should try as much as possible, all representative data for your inputs and see their effect on your program. This way, in every generation of your testing, you will finally fine-tune your program and you'll become more confident with its execution. The only way to justify confidence in a program is to program carefully and so avoid most errors.

2 comments:

john18 said...

How do you call the kind of program error wherein you have mistaken the structure of a program? Like I have this error in my program which really pissed me, where I should have placed some parts of my program above everything. Hope you can answer me, our teacher actually in our programming class made this term as a homework for us to research. Thanks!

j03m4r13 said...

Thanks for the post.. Anyway, upon reviewving your question of the kind of program error you're asking, I believe, that you are talking about the order or sequence of the program. Well, that is certainly a type of Logic Error in your program. As you can see, most of the time, your program will still execute, but you get different results as to what you are expecting. Mostly, that's the manifestation of a logic error in your programs. Like mathematical precedence, remember the MDAS thing? Well, if you don't know the Order of the operators, sometimes the results are erroneous in our program. So, that's the best answer. Logic Errors!