Wondering why JavaScript doesn't include exception-handling constructs like its bigger cousins? Well, the newest version of JavaScript does - and this article tells you all about it, explaining how you can use the new Error object and the "try-catch" constructs to trap and resolve errors in script execution.
And that just about covers it. In this article, I took you on a guided tour of exception handling in JavaScript, demonstrating how the "try" and "catch" blocks can be used to trap and resolve errors in script execution. I showed you how to retrieve the exception type and message, write exception handlers for different error types, and even generate your own custom errors. Finally, I wrapped things up with a real-world example that demonstrated how to build a simple form validator and generate exceptions suitable for processing by a generic "try-catch" handler.
In case you'd like to learn more about these topics, you should consider visiting the following links:
The official ECMAScript reference, at http://www.ecma-international.org/publications/standards/ecma-262.htm
JavaScript 1.5 reference material in the Netscape DevEdge documentation, at http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/st mt.html#1051663
Handling errors in Internet Explorer, at http://msdn.microsoft.com/library/en-us/dnscrpt/html/WebErrors2.asp
Until next time...be good!
Note: Examples are illustrative only, and are not meant for a production environment. Code samples in this article have been tested on Microsoft Internet Explorer 6.0. Melonfire provides no warranties or support for the source code described in this article. YMMV!