1 / 17

2.5 Errors

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.

tgilmore
Download Presentation

2.5 Errors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2.5 Errors

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. 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); • …

  11. 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.

  12. 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.

  13. 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.

  14. 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.

  15. 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.

  16. The End

More Related