170 likes | 184 Views
2.5 Errors. Programming errors can be classified in several ways. One way of classifying them is by when they are detected, before, during, or after a run. Another way, which is related, is by whether they are errors of syntax or errors of logic.
E N D
Programming errors can be classified in several ways. • One way of classifying them is by when they are detected, before, during, or after a run. • Another way, which is related, is by whether they are errors of syntax or errors of logic.
Syntax errors are mistakes in using the rules of the language. • Logic errors are mistakes in understanding the problem and coming up with a set of steps to solve it. • Here is a table with some general information about these categories of errors.
The three most common, simplest, and often most vexing syntax errors for beginners are the following: • 1. Forgetting a semicolon at the end of a line of code. • This can be troublesome because the error message will indicate that the error is in a neighboring line, not the line missing the semicolon.
2. Forgetting to match braces. • This can also be troublesome because the compiler will not correctly identify the line where the error is. • If you indent matched braces when writing programs, this decreases the chance of making this mistake and increases the chance of finding it easily if it is made.
3. The fact that Java is case sensitive can also be troublesome. • Forgetting to capitalize something that should be capitalized or mistakenly capitalizing something that shouldn’t be capitalized will lead to problems. • It is difficult for the human eye to detect such minor differences in code.
Mistakes in capitalization are especially problematic. • Quite often they are simply typographical errors, not logic errors on the part of the programmer. Sometimes they result in a syntax error which the compiler will detect or which will be detected at run time. • However, they may also result in apparently correct code and the error will only become apparent at run time, as if it were a logic error.
In other words, these mistakes cause problems that seem to straddle the boundary between syntax and logic errors. • For this reason and because it is not easy to see either a missing or unintentional capital letter, they can be hard to track down.
You may already have noticed that it is customary to capitalize the first letter of the names of classes, but not of object references. • For object references, such as myPoint, it is also customary to capitalize internal letters in order to bring out the subparts of a name. • These conventions make capitalization errors possible. • However, the conventions are useful because when you read code you can quickly recognize class and object names.
In a language where objects are constructed and references to objects are declared, there is another basic mistake that programmers can make. • This is the attempt to use references when an object has not yet been constructed. • Consider the following fragment of code, which contains this kind of error: • … • Point yourPoint; • System.out.println(yourPoint); • …
In this example, you declare a reference but do not construct an object. • The reference doesn’t refer to anything. • In the code, this reference is passed to the println() method as a parameter. • When you try to compile it, the compiler will give an error saying that yourPoint has not been initialized.
Now consider the following example: • … • Point yourPoint; • yourPoint.translate(30, 40); • … • This time you’re calling a method on a reference which does not refer to an object. • The compiler will also find this error and say that yourPoint hasn’t been initialized.
The Java compiler and run time environment try to detect errors and print helpful error messages. • However, they can’t find everything and sometimes the messages are somewhat cryptic.
As more powerful concepts and syntax become available, and as programs grow more complex, programming errors can become more subtle in nature and the programmer has to be careful when programming and attentive when trying to debug faulty programs.
Object references are a critical part of Java programming. • Their correct use and errors resulting from their misuse will be covered in greater detail in future units of these notes.