Home arrow JavaScript arrow Page 3 - Exception Handling in JavaScript: Using Multiple Exception Handlers
JAVASCRIPT

Exception Handling in JavaScript: Using Multiple Exception Handlers


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.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
September 20, 2005
TABLE OF CONTENTS:
  1. · Exception Handling in JavaScript: Using Multiple Exception Handlers
  2. · Handling multiple errors: stepping back to the first example
  3. · Working with primary error types: defining error types for the JavaScript 1.5 specification
  4. · A practical example: trapping multiple error types
  5. · Using multiple handlers: working with “if-else if” blocks

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Exception Handling in JavaScript: Using Multiple Exception Handlers - Working with primary error types: defining error types for the JavaScript 1.5 specification
(Page 3 of 5 )

In normal conditions, you may want to set up exception handlers for the most common error types found during the execution of a program. Considering this, JavaScript interpreters that are fully compliant with the 1.5 specification, define six primary error types, categorized in the following list:

EvalError: triggered by the interpreter when the eval() function fails to evaluate its argument.

RangeError: triggered by the interpreter when a numeric variable exceeds its allowed range.

ReferenceError: triggered by the interpreter when an invalid reference is used (applicable to data containers as well as methods or functions).

SyntaxError: triggered when the interpreter finds a syntax error while parsing JavaScript code.

TypeError: triggered when the type of a variable is incorrect.

URIError: triggered when the encodeURI() and decodeURI() functions receive invalid arguments.

As you can see, the above list provides enough support to handle common errors in a very efficient way. Even in the case where you might want to set up an exception handler for an error not contemplated on the list, JavaScript will allow you to create your own custom exception and implement the corresponding handler, by using the built-in Error constructor.

So far, I’ve coded some primitive examples of exception handlers, which only generated simple error messages without giving more descriptive information on the error that occurred. However, there is more good news in the JavaScript universe. In addition to providing support for primary error types, JavaScript also offers two predefined properties of the Error object generated by an exception, the “name” and “message” properties.

Considering JavaScript the exception capabilities just described, let’s turn our attention to building an example for seeing how they can be properly implemented within a simple program.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials