JavaScript
  Home arrow JavaScript arrow Page 3 - Exception Handling in JavaScript: Introduc...
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  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
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: Introduction to Core Concepts
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2005-09-07

    Table of Contents:
  • Exception Handling in JavaScript: Introduction to Core Concepts
  • Exceptional code: introducing the basics of JavaScript exceptions
  • Trapping exceptions: explaining “try-catch” blocks
  • A basic example: handling exceptions through a generic mechanism

  • 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: Introduction to Core Concepts - Trapping exceptions: explaining “try-catch” blocks


    (Page 3 of 4 )

    True to form, the official JavaScript syntax to create an exception is with the “throw” statement:

    throw expression;

    And, to handle an exception, it uses the standard combination of “try-catch” blocks:

    try {

        execute this block

    }

    catch (error) {

        execute this block if an error has occurred

    }

    It should be noted that in a weakly typed language like JavaScript, an exception can be anything (from plain strings to objects), but unless a specific exception type is launched by a “throw” statement, whenever an error arises, JavaScript makes automatically available an Error object, which is created when an error is triggered.

    Additionally, JavaScript supports handling of multiple exceptions through two different structures. First, by using multiple “try-catch” blocks, as follows:

    try {

        execute this block

    }

    catch (error type1) {        

        execute this block if an error of type 1 has occurred

    }

    catch (error type2) {

        execute this block if an error of type 2 has occurred

    }

    finally {                   

        execute this block after the try block

    }

    And second, different types of exceptions can be trapped by using multiple “if” statements within a single “catch” block:

    try {

        execute this block

    }

    catch (error) {

        if(error instanceOf error type1) { 

            execute this block if an error of type 1 has occurred

        }

        else if(error instanceOf error type2) { 

            execute this block if an error of type 2 has occurred

        }

        else if(error instanceOf error type3) { 

            execute this block if an error of type 3 has occurred

        }

        else {

            execute this block is none of the above errors occurred

        }

    }

    Although both forms of syntax are valid for dealing with multiple exceptions, the last one listed is by far more popular because of its close intuitive similarity with “if-else if” control blocks found on most programming languages.

    Now, allow me to explain how the flow of a program is handled in each pertinent case. In the first case, if an exception is found when executing the code within the “try” block, the JavaScript interpreter will stop the program execution at that point and search for a “catch” block that contains the proper exception handler. After the exception has been handled or no catch blocks present a matching exception handler, then program execution is transferred to the “finally” block.

    In the second case, program flow is handled in a similar way. If an exception is encountered during the execution of the “try” block, the program is stopped at that point and JavaScript will look for an exception handler that matches the type of error triggered, within the successive “if-else if” blocks. If the exception has been handled within an “if” block, (and assuming that the program hasn’t been halted), control is returned to the code following the ‘try” statement. Otherwise, if no exception handler is found on each “if” structure, the code within the final “else” block is executed.

    To demonstrate how this process works, an example is preferred in all cases, so let’s jump over the next lines to see a few pieces of illustrative code.

    More JavaScript Articles
    More By Alejandro Gervasio


       · The first part of the series goes through the explanation of basics exception...
     

    JAVASCRIPT ARTICLES

    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway