Now that you are writing programs, you are going to encounter errors. Some will be careless typos while others will be more serious developmental errors.
There are three types of errors.
1. Compile-time Errors (Syntax errors): These are errors that violate coding rules.
• Maybe you forgot a semicolon at the end of a line.
•
Maybe you declared a variable called "numbers", but then you initialize a variable called "number".
• Maybe you initialized a variable that had not yet been declared.
• Maybe you forgot to close a set of quotes.
• Maybe you capitalized where you should not have, or vice versa.
You get the idea. These are the easiest errors to find.
Look for a red error mark on the left side of your Eclipse source code window.
2. Logic Errors: These errors are what are typically referred to as "bugs".
The syntax may be correct, but the program does not correctly accomplish its task.
• Maybe you were asked to find the number of inches in a foot, but you divided by 6 instead of 12.
You will notice this error because your output will yield the wrong results.
Sometimes "bugs" are easily found, such as this division problem. Other times, the process of finding a bug (debugging) can be difficult and time consuming. It is often hard to find an error in something we wrote ourselves. One method of searching for a bug is called "tracing" the program. This is where you examine what is occurring in the computer's memory to see if it is doing what you think it is doing in relation to variables in the program. Tracing can be done by hand (tedious), or it can be done by inserting print lines at various locations in a program to see if the variables are performing as we expect.
3. Run-time Errors: These errors occur when we ask the computer to do something it considers to be illegal.
• Maybe you unknowingly set up a situation where division by zero occurs.
• Maybe you spelled "main" as "Main" by mistake.
These errors STOP the execution of the program. To find and fix these errors, start by going to the line in the source code that was running when the crash occurred.
For now, let's look at what we are seeing in Eclipse:
Even before compiling a program, Eclipse attempts to help you find problems with your programs. When an error is observed, a red X appears in the leftmost column in the editor window. Double clicking on the red X will show a description of the problem. Sometimes these "hints" will be easily understood, and other times they may be a bit cryptic.
The error in this program is that the variable number was declared, but we are attempting to use a variable called numbers.
A single click on the red X will display the message
"numbers cannot be resolved to a variable".
A double click will give additional information. Notice that the yellow box to the right is suggesting that we declare numbers as an int. The box on the left is offering possible options.
|