Home arrow JavaScript arrow Page 2 - 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 - Handling multiple errors: stepping back to the first example
(Page 2 of 5 )

As you probably remember, in the first article I showed a basic example that demonstrated how to handle two specific exceptions, which were thrown within a simple function. To quickly refresh the concepts, the respective function looked like this:

function loanCalculator(loanAmount,loanTerm){
    if(!loanAmount||parseFloat(loanAmount)<10000||parseFloat
(loanAmount)>300000){
        // throw exception
        throw 'Loan amount must be between $10000 and $100000';
    }
    if(!loanTerm||parseInt(loanTerm)<1||parseInt(loanTerm)>30){
        // throw exception
        throw 'Loan term must be an integer positive value
between 1 and 30';
    }
    return parseFloat(loanAmount)/parseInt(loanTerm);
}

And, using the “try-catch” blocks, it could be called with the following lines:

try{
    loanCalculator(10000,45);
}
// catch thrown exceptions
catch(e){
    if(e=='Loan amount must be between $10000 and $100000'){
        // do something to process this error
        alert(e);
    }
    else if (e=='Loan term must be an integer positive value
between 1 and 30'){
        // do something else to process this error
        alert(e);
    }
}

Even when the sample code has been shortened, it’s clear to see how two specific exceptions are trapped within the “catch” block. In particular, the function throws a custom exception whenever any of its arguments is considered invalid, by sending an error message as part of the exception itself.

The example shows a mechanism for trapping two different errors, which should be handled in different ways. Of course, the didactical sense of the above listed code resides on the support that JavaScript offers to handling multiple exceptions. Things get even more interesting by considering that the JavaScript 1.5 specification defines a set of six primary error types, which can be easily trapped through specific exception handlers.

Considering this propitious scenario, let’s have a look at the list of error types defined within the JavaScript 1.5 specification, along with some examples of how to catch them within an application.


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