Programming Errors
What You Will Learn
  • What is a syntax error
  • What is a run-time error
  • What is a logic error
  • What is desk checking
  • What is a tracing
  • What is a debugging

According to an old saying, we learn from our mistakes, which is fortunate because most people find it almost impossible to write even simple programs without making numerous mistakes. These mistakes, or errors, are of three types: syntax errors, run-time errors, and logic errors.

The Three Types of Errors

Syntax Errors

Syntax errors, occur when we violate a syntax rule, no matter how minor. These errors are detected at compile time. For instance, if a semicolon is missing at the end of a statement or if a variable is used before it is declared, the compiler is unable to translate the program into byte code. The good news is that when the Java compiler finds a syntax error, it prints an error message, and we can make the needed correction. The bad news is that the error messages are often quite cryptic. Knowing that there is a syntax error at a particular point in a program, however, is usually a sufficient clue for finding the error.

Run-time Errors

Run-time errors occur when we ask the computer do something that it considers illegal, such as dividing by 0. For example, suppose that the symbols x and y are variables. Then the expression x/y is syntactically correct, so the compiler does not complain. However, when the expression is evaluated during execution of the program, the meaning of the expression depends on the values contained in the variables. If the variable y has the value 0, then the expression cannot be evaluated. The good news is that the Java run-time environment will print a message telling us the nature of the error and where it was encountered. Once again, the bad news is that the error message might be hard to understand.

Logic Errors

Logic errors (also called design errors or bugs) occur when we fail to express ourselves accurately. For instance, in every day life, we might give someone the instruction to turn left when what we really meant to say is to turn right. In this example

The bad news is that programming environments do not detect logic errors automatically. The good news is that there are strategies you can use to prevent logic errors and detect them when they occur.

Error Detection

Desk Checking

One strategy we can use to reduce the number of logic errors in a program is to reread the code carefully after we have written it. This is called desk checking and is best done when the mind is fresh. Because programming requires exhausting and excruciating attention to detail, avoid programming for long stretches of time or when tired, a rule you will break frequently unless you manage your time well.

Debugging

After we have established that a program contains a logic error, or bug as it is more affectionately called, we still have the problem of finding it. Sometimes the nature of a bug suggests its general location in a program. We then can reread this section of the program carefully with the hope of spotting the error. Unfortunately, the bug often is not located where we expect to find it, and even if it is, we will probably miss it. After all, we thought we were writing the program correctly in the first place, so when we reread it, we tend to see what we were trying to say rather than what we actually said.

Tracing

Programmers, as a consequence, are frequently forced to resort to a rather tedious, but powerful, technique for finding bugs called tracing. We add to the program extra lines of code that print the values of selected variables in the terminal window. Of course, we add these lines where we anticipate they will do the most good - that is, preceding and perhaps following places in the program where we think the bug is most likely located. We then run the program again, and from the extra output, we can determine if any of the variables deviate from their expected values. If one of them does, then we know the bug is close by, but if none do, we must try again at a different point in the program. A variable's value is printed in the console window as follows:

Look at the following examples:

System.out.println("some message" + variable name);

Example

If at a certain point in a program you expect the variable num to have a value of 10. You could add code like the following:

System.out.println("num (10) = " + num);

When this line is printed on the console window if the variable looks like

num (10) = 10

then you know the problem is not occurring at this spot in your program. If on the other hand, if looks like this

num (10) = 7

then you know the error is located somewhere above this line of code in your program.