JavaScript
  Home arrow JavaScript arrow Page 2 - Exception Handling in JavaScript: Using Mu...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Exception Handling in JavaScript: Using Multiple Exception Handlers
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2005-09-20

    Table of Contents:
  • Exception Handling in JavaScript: Using Multiple Exception Handlers
  • Handling multiple errors: stepping back to the first example
  • Working with primary error types: defining error types for the JavaScript 1.5 specification
  • A practical example: trapping multiple error types
  • Using multiple handlers: working with “if-else if” blocks

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More JavaScript Articles
    More By Alejandro Gervasio


       · This second tutorial is primarily focused on explaining how to use multiple...
     

    JAVASCRIPT ARTICLES

    - Using jQuery to Preload Images with CSS and ...
    - Using Client-Side Scripting to Preload Image...
    - Removing Non-Semantic Markup when Preloading...
    - Using the Display CSS Property to Preload Im...
    - Preloading Images with CSS and JavaScript
    - Scaling and Moving Web Page Elements with th...
    - Fading, Hiding and Sliding HTML Elements wit...
    - Toggling CSS Properties with the GX JavaScri...
    - Cancel, Queue and Pause Animations with the ...
    - Producing Elastic Effects with the GX JavaSc...
    - Moving Divs Diagonally with the GX JavaScrip...
    - Moving Elements Vertically and Horizontally ...
    - Making Bouncing Effects in Parallel with the...
    - Creating Bouncing Effects with the GX JavaSc...
    - Manipulating Background Colors with the GX J...







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek