Home arrow JavaScript arrow Page 4 - 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 - A practical example: trapping multiple error types
(Page 4 of 5 )

Having described the main features associated with JavaScript primary error types, what I’ll do next is write a simple function, which adds on the fly a bunch of links to the document tree by using some DOM methods. This function implements a single exception handler, and its definition looks like this:

function addLinks(){
    // define links
    try {
        var links=new Array
('home.htm','profile.htm','products.htm','contact.htm');
        for(var i=0;i<links.length;i++){
            // create <a> elements
            var as=document.createElement('a');
            // add href property
            as.href=links[i];
            // add title property
            as.title=links[i].replace(/.htm/,'');
            // add link labels
            as.appendChild(document.createTextNode(' '+as.title+' '));           
           // add links to document tree
            document.body.appendChild(as);
        }
    }
    catch(e){
        var p=document.createElement('p');
        p.appendChild(document.createTextNode('An exception was
thrown by the script. Error name :'+e.name+' Error
message :'+e.message));
        document.body.appendChild(p);
    }
}
// call function when page is loaded
window.onload=function(){
    var W3C=document.getElementById&&document.
createElement&&document.getElementsByTagName;
    if(W3C){
        addLinks();
    }
}

As I mentioned before, the “addLinks()” function appends dynamically some links to the document tree, defined through the “links” array, and sets up a single exception handler. Within the “catch()” block, the type of error is displayed by adding a paragraph, which is populated with the values of  “name” and “message” Error’s object properties.

When the script is run, it outputs a group of regulars links and nothing else, because the code has been correctly executed. The script’s output is depicted below:

Now, let’s tweak the code a little bit and introduce some changes for seeing how the script reacts to them. So, firstly I’ll deliberately replace the line:

var as=document.createElement('a');

with this one:

var a=document.createElement('a');

After running the script, what I get on my browser is the following output:

An exception was thrown by the script. Error name :ReferenceError
Error message :as is not defined

Since I replaced the “as” variable with “a”, the function throws a reference error, because the script fails to find the referenced variable.

Next, let’s make the script trap a type error. To do this, I’ll simply replace the line:

var links=new Array
('home.htm','profile.htm','products.htm','contact.htm');

with this one:

var links=new Array
(1,'profile.htm','products.htm','contact.htm');

In this case the function launches a type error, which is trapped by the “catch” block, as follows:

An exception was thrown by the script. Error name :TypeError
Error message :links[i].replace is not a function

The raised error is pretty clear, because the “links” array’s first element has been set to be numeric, so the “replace()” method fails in replacing a string value.

By this point, you should have a clear idea of how the single exception handler works. By deliberately introducing some changes within the script, I caused several errors to be raised, and accordingly the handler has trapped them.

Now that a single exception handler was implemented on the first example, let’s move on and use the same script, this time using multiple handlers.


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