Lesson 8:
1. SYNTAX ERROR
2. RUNTIME ERROR
3. LOGIC ERRORS.
1. Syntax Errors
Errors that are detected by the compiler are called syntax errors or compile errors. Syntax errors result from errors in code construction, such as mistyping a keyword, omitting some necessary punctuation, or using an opening brace without a corresponding closing brace. These errors are usually easy to detect because the compiler tells you where they are and what caused them. For example, in listing 1.4 as shown in Figure 1.10 below.
Four errors are reported, but the program actually has two errors:
■ The keyword void is missing before main in line 2.
■ The string Welcome to Java should be closed with a closing quotation mark in line 3.
Since a single error will often display many lines of compile errors, it is a good practice to fix errors from the top line and work downward. Fixing errors that occur earlier in the program may also fix additional errors that occur later.
Tip
If you don’t know how to correct it, compare your program closely, character by character, with similar examples in the text. In the first few weeks of this course, you will probably spend a lot of time fixing syntax errors. Soon you will be familiar with Java syntax and can quickly fix syntax errors.
2. Runtime Errors
Runtime errors are errors that cause a program to terminate abnormally. They occur while a program is running if the environment detects an operation that is impossible to carry out. Input mistakes typically cause runtime errors. An input error occurs when the program is waiting for the user to enter a value, but the user enters a value that the program cannot handle.
For instance, if the program expects to read in a number, but instead the user enters a string, this causes data-type errors to occur in the program.
Another example of runtime errors is division by zero. This happens when the divisor is zero for integer divisions. For instance, the program in Listing 1.5 would cause a runtime error, as shown in Figure 1.11.
1.10.4 Common Errors
Missing a closing brace, missing a semicolon, missing quotation marks for strings, and misspelling names are common errors for new programmers.
Common Error 1: Missing Braces
The braces are used to denote a block in the program. Each opening brace must be matched by a closing brace. A common error is missing the closing brace. To avoid this error, type a closing brace whenever an opening brace is typed, as shown in the following example.