JavaScript
  Home arrow JavaScript arrow Page 7 - JavaScript Exception Handling
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
eWeek
 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

JavaScript Exception Handling
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 150
    2003-08-14

    Table of Contents:
  • JavaScript Exception Handling
  • Anatomy Of An Exception
  • Playing Catch
  • Being Verbose
  • All For One...
  • The Final Solution
  • Raising The Bar
  • Endzone

  • 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
     
     
    Iron Speed
     
    ADVERTISEMENT

    Ziff Davis Enterprise Virtual Tradeshows: Hot Topics, Cutting Edge Technology, Real-time Networking among IT Professionals. Learn more

    JavaScript Exception Handling - Raising The Bar
    (Page 7 of 8 )

    Thus far, you've been working with JavaScript's built-in exceptions, which can handle most logical or syntactical errors. However, JavaScript also allows you to get creative with exceptions, by generating your own custom exceptions if the need arises.

    This is accomplished via JavaScript's "throw" statement, which is used to raise errors which can be detected and resolved by the "try" family of exception handlers. The "throw" statement needs to be passed an error type. When the exception is raised, this exception name and description will be made available to the defined exception handler.

    Let's go to a quick example. In the following piece of code, if the value entered into the form field is greater than 99, the code will manually generate a RangeError exception, which will be caught and displayed appropriately.

    
    <html>
    <head>
    <script language="JavaScript">

    function checkAge()
    {
    try {
    // if incorrect value in form
    // generate an error
    if (document.forms[0].age.value > 99)
    {
    throw RangeError;
    }
    } catch (e) {
    // catch all thrown errors and print error type
    alert(e.name);
    }
    }

    </script>
    </head>

    <body>

    <form>
    <input type="text" name="age" onBlur="checkAge()">
    </form>

    </body>
    </html>

    You can also create your own exceptions, via the JavaScript Error constructor. The following example demonstrates, by creating a custom error type called idiotUserError.

    
    
    

    In this case, when you run the script, a new Error object will be created named idiotUserError, with the message specified in the Error object constructor. This error can now be thrown using the regular "throw" statement, as in the example above.

    Let's try a more useful example:

    
    <html>
    <head>
    <script language="JavaScript">

    // create new Error objects
    badNameError = new Error ("System user name does not match actual user name"); noNameError = new Error ("System user name cannot be blank");

    // create generic exception handler
    // to display error message
    // you may want to filter down error types
    // further inside this and handle each type
    // differently
    function mainExceptionHandler(e)
    {
    alert (e.message);
    }

    // general form validation functions
    function validateForm()
    {
    checkName();
    }

    // check form values
    // if errors, throw appropriate exception
    function checkName()
    {
    try {
    if (document.forms[0].username.value == "")
    {
    throw noNameError;
    }

    if (document.forms[0].username.value != "john")
    {
    throw badNameError;
    }
    } catch (e) {
    // any and all errors will go to this handler
    mainExceptionHandler(e);
    }
    }

    </script>
    </head>

    <body>

    <form onSubmit="validateForm()">
    Enter your system username: <input type="text" name="username"> <br> <input type="submit" name="submit" value="Go"> </form>

    </body>
    </html>

    In this case, if the value entered into the form is blank or not "john", a custom exception will be thrown by the code. This custom exception can be caught by a generic exception handler such as the one used above, and the exception can be routed and resolved appropriately

    More JavaScript Articles
    More By icarus, (c) Melonfire


       · What about window.onerror?Errors may be any place in code, and this article seem...
       · Here, for your reference, is the meaning of the...
     

       

    JAVASCRIPT ARTICLES

    - Getting Attention with Interactive Effects
    - Interacting with Tooltips and Previews
    - Just-in-Time Information and Ajax
    - Interactive Effects
    - Using Cookies With JavaScript
    - Understanding the JavaScript RegExp Object
    - Controlling Browser Properties with JavaScri...
    - Using Timers in JavaScript
    - Form Validation with JavaScript
    - JavaScript Exception Handling
    - Stringing Things Along
    - Understanding The JavaScript Event Model (pa...
    - Understanding The JavaScript Event Model (pa...
    - An Object Lesson In JavaScript

    Iron Speed



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