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 / 9
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

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

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




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