|
December 9, 1999 Exception Handling Tips: December 1999
Yehuda Shiran, Ph.D.
|
|
In any programming language, your code will throw a run-time exception when trying to execute an illegal command. One of the most trivial illegal operations you may attempt is a division by zero. Your operating system will definitely complain when trying to divide a number by zero. This is not a good example in JavaScript, as JavaScript does execute the operation and assigns infinity to the resulting variable. Other illegal operations are common to most programming languages, including JavaScript. Accessing a null pointer or a null object is one example. Accessing an array element that is out of the array's legal range is another.More often than not, you want to avoid system errors. System messages are usually cryptic and do not make sense for the average user. In fact, system messages are bad for your reputation as a programmer, as they are clear and tangible evidence of your bugs. Naturally, you'll want to avoid these messages by checking for them in your code, before they hit the operating system. Checking for exceptions in your code will surely make it more cumbersome.
The crux of the exception handling support in JavaScript is the Here is an example that you can actually run on your computer (Internet Explorer 5.0 and up):
The exception enforced in this example is accessing an array element that is outside the array boundaries. The highest index used is
People who read this tip also read these tips: Look for similar tips by subject: |