Exception Handling in JavaScript: Using Multiple Exception Handlers
(Page 1 of 5 )
In this article, you will learn how to work with multiple exception handlers in JavaScript, and deal with primary error types. This will be illustrated by several examples, to get you familiar with JavaScript exceptions.
Welcome to the second part of the series “Exception Handling in JavaScript”. Throughout the first article of the series, I introduced the basics of JavaScript exceptions by explaining the role of the “throw” statement, together with the popular exception handling combination comprised of “try-catch” blocks.
In conjunction with the explanations about the corresponding core concepts, I set up some illustrative examples, in order to demonstrate how simple application layers can be conceived and written by establishing a logic aimed at clearly delineating code responsibility. As described previously, exceptions allow you to easily separate code for dealing with normal program execution from routines specifically programmed to handle raised errors.
It should be noted, though, that the examples were extremely simple and implemented a basic approach for handling a few triggered errors. In general, it’s a poor programming practice to catch all errors without checking their type, or even worse, ignore them completely. That’s why I suggest that you start using “try-catch” blocks within your JavaScript programs, and configure the appropriated exception handler accordingly, to treat specific types of errors.
Since different errors could occur during the execution of an application, the topic brings us to the subject of this second part. Over the course of this article, I’ll explain specific types of errors that JavaScript interpreters may eventually trigger, together with the implementation of multiple exception handlers, in order to trap errors through a more professional technique.
With the goals of the article already established, let’s move forward to learn more about JavaScript exceptions.
Next: Handling multiple errors: stepping back to the first example >>
More JavaScript Articles
More By Alejandro Gervasio