Java Reference
In-Depth Information
1.10 Programming Errors
Programming errors can be categorized into three types: syntax errors, runtime
errors, and logic errors.
Key
Point
1.10.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, the program in Listing 1.4 has a syntax error, as shown in
Figure 1.10.
syntax errors
compile errors
L ISTING 1.4
ShowSyntaxErrors.java
1 public class ShowSyntaxErrors {
2 public static main(String[] args) {
3 System.out.println( "Welcome to Java );
4 }
5 }
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.
Compile
F IGURE 1.10
The compiler reports syntax errors.
Tip
If you don't know how to correct it, compare your program closely, character by char-
acter, 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.
fix syntax errors
1.10.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
runtime errors
 
 
 
Search WWH ::




Custom Search