Exception Handling in JavaScript: Introduction to Core Concepts
(Page 1 of 4 )
JavaScript 1.5 remedied an omission of earlier versions by including built-in support for advanced exception handling. Of course, just because the support is there doesn't mean that developers will enjoy error-free code! For this reason, JavaScript exceptions should be properly considered when writing applications. This article is intended to provide you with the basics about exceptions.
For most programmers accustomed to working with a thoroughly object-oriented language like Java, exceptions are long-established programming structures that allow them to handle multiple runtime errors through a centralized mechanism within client code. Certainly, this method for handling errors has proven to be highly successful in the vast majority of applications, which turned it rapidly into the standard technique for delegating error manipulation to specific fragments of code, different from those that generate error conditions.
Particularly, JavaScript was one of the client-side scripting languages that wasn’t “blessed” with a powerful built-in exception mechanism, so for most developers, tracking script errors was for a long time an experience tightly tied to including alerts inside the code or settling down with browser-based error debuggers, such as the popular “JavaScript console.” Although all these tools together have significantly simplified error tracking and debugging, still JavaScript programs lacked true support for exceptions.
With the release of JavaScript 1.5 (available from most sixth generation browsers onward), built-in support for advanced exception handling is available to all developers, allowing them to write more robust code by using popular “try-catch” blocks, as well as working with “throw” statements for complex user-defined exceptions.
Of course, wrapping up all the code into “try-catch” blocks doesn’t make sure that JavaScript programs will be error-free at all, but it does implement a strong mechanism for solving errors within the application layer, all without messing up the code with old-fashioned error checking routines.
It’s precisely for that reason that JavaScript exceptions should be properly considered when writing applications, according to the tendencies that modern Web programming dictates. Considering this scenario, this article is intended mainly to provide you with the basics about exceptions, by explaining its benefits and exploring its implementation.
Also, the series will cover the most common error types found during program execution, in conjunction with some JavaScript constructs useful for solving them gracefully. Since only theoretical concepts are rather useless on their own, numerous examples will be explained, so you can start using exceptions in your JavaScript programs.
Next: Exceptional code: introducing the basics of JavaScript exceptions >>
More JavaScript Articles
More By Alejandro Gervasio