Exception Handling in JavaScript: Catching User Input
(Page 1 of 4 )
In a previous article, Alejandro Gervasio explained some key concepts related to working with primary error types, and how they are raised by JavaScript interpreters. This article will focus on developing specific sample code for trapping most of the primary error types according to data entered by users.
Introduction
This is the third part of the series “Exception handling in JavaScript.” Welcome back. Throughout the previous part, I explained some key concepts related to working with primary error types, along with using multiple exception handlers for trapping different raised errors. Of course, numerous examples were demonstrated, aimed primarily at exposing clearly how most primary error types can be deliberately triggered. In this way, it’s possible to implement the proper error handlers and see how JavaScript code can react and handle unexpected conditions during program execution.
If you’ve been playing around with the examples that I showed in the previous tutorial, then you already have a pretty clear idea of how primary error types are raised by the vast majority of JavaScript interpreters. Although sample code isn’t meant for the production environment, it does illustrate core concepts inherent to implementing “try-catch” blocks within JavaScript programs, with the goal of making them look more professional from a programming point of view. As you know, exceptions allow you to establish specific fragments of code for trapping raised errors, while maintaining “regular” routines focused on dealing with normal and expected situations.
This powerful ability can make a big difference, particularly when working with large applications, where centralized error handling is often required, by introducing a considerable impact on JavaScript code development.
By returning to the objectives of this third part of the series, strong attention will be put on developing specific sample code focused on trapping most of the primary error types according to data entered by users. Hopefully the experience will be instructive enough to illustrate how primary errors can be caught by the appropriate error handlers, based on user-provided input.
Now that you have a clear idea of the topics to be treated in this article, we can move on to writing the complete scripts for working with user-supplied data.
Next: Using multiple error handlers: a quick overview of the previous script >>
More JavaScript Articles
More By Alejandro Gervasio