JavaScript
  Home arrow JavaScript arrow Page 3 - 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 - 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.

    More JavaScript Articles
    More By Alejandro Gervasio


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

    JAVASCRIPT ARTICLES

    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in
    - Active Client Pages at the Server
    - ACP Tab Web Page







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT