Java Reference
In-Depth Information
1.7 Errors
Experiment a little with the HelloPrinter program. What happens if you make a
typing error such as
System.ouch.println("Hello, World!");
System.out.println("Hello, World!);
System.out.println("Hello, Word!");
In the first case, the compiler will complain. It will say that it has no clue what you
mean by ouch . The exact wording of the error message is dependent on the compiler,
but it might be something like ȒUndefined symbol ouchȓ. This is a compile-time error
or syntax error. Something is wrong according to the language rules and the compiler
finds it. When the compiler finds one or more errors, it refuses to translate the program
to Java virtual machine instructions, and as a consequence you have no program that
you can run. You must fix the error and compile again. In fact, the compiler is quite
picky, and it is common to go through several rounds of fixing compile-time errors
before compilation succeeds for the first time.
A syntax error is a violation of the rules of the programming language. The
compiler detects syntax errors.
If the compiler finds an error, it will not simply stop and give up. It will try to report as
many errors as it can find, so you can fix them all at once. Sometimes, however, one
error throws it off track. This is likely to happen with the error in the second line.
Because the closing quotation mark is missing, the compiler will think that the );
characters are still part of the string. In such cases, it is common for the compiler to
emit bogus error reports for neighboring lines. You should fix only those error
messages that make sense to you and then recompile.
The error in the third line is of a different kind. The program will compile and run, but
its output will be wrong. It will print
Hello, Word!
This is a run-time error or logic error. The program is syntactically correct and does
something, but it doesn't do what it is supposed to do. The compiler cannot find the
Search WWH ::




Custom Search