Team LiB
Previous Section Next Section

Understanding Error Messages

Now that you know how to display error messages, you're probably wondering what the message means. People in the computer industry used to joke about error messages because these messages were generally (and sometimes still are) short, confusing, and oftentimes of no use whatsoever. It would not be uncommon for an early program writer to get an error similar to "Error #3810482," which contains no useful error message at all. Error messages have become more sophisticated and useful since they first began appearing, but they can still sometimes be quite confusing.

JavaScript error messages generally have three parts to them. If you look at Figures 13.2 and 13.5, you will notice that the figures both show the name of the file that caused the error, C:\JPP\Error.html; the line on which the error was detected, 22; and the actual error message,

myObject is not defined.

The first part of every error message, the path to the file that caused the error, is useful only if you are using a multiple-page Web site. If you're building a site that has multiple frames or that shares functions between pages, this error information will narrow your search for the cause of the error down to one page.

The second part of every error message is the line number on which the error was detected. Unfortunately, there is a big difference between which line caused the error and in which line the error was detected. In the running example so far, the line number is helpful because line 22 was the line in which I used an object instance myObject before the variable was actualy defined. This is not always the case. One notable exception of an error message giving the wrong line number is when you forget to end a string literal with a quotation mark. The interpreter will not normally catch the error on the line that is missing a quotation—it will catch it in the next <script> block of the Web page. As a rule of thumb, the error in question usually occurred on the line number that was given in the error message or shortly before it.

The last part of every error is a message defining the type of error that occurred. This part of the error message is usually self-explanatory, but it can occasionally be confusing if you aren't familiar with every aspect of the JavaScript language. If the message is helpful, then great, go fix the error. If the message is confusing, don't worry about it—the first two parts of the error message will usually provide enough information to fix the error.

Debugging is not an easy task. Nobody writes a program perfectly the first time through, which makes debugging a useful and necessary skill to acquire.

If you encounter an error that you are having difficulty fixing, try these simple steps to get you started:


Team LiB
Previous Section Next Section