JavaScript
  Home arrow JavaScript arrow Page 4 - Exception Handling in JavaScript: Catching...
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: Catching User Input
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2005-09-27

    Table of Contents:
  • Exception Handling in JavaScript: Catching User Input
  • Using multiple error handlers: a quick overview of the previous script
  • Catching user data: triggering errors through web forms
  • A quick and dirty error handler: using JavaScript prompts

  • 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: Catching User Input - A quick and dirty error handler: using JavaScript prompts


    (Page 4 of 4 )

    Once you’ve learned how to generate most of the primary error types through regular web forms, it’s extremely easy to rewrite the above described “showError()” function, in order to test potential errors. Of course, I won’t bore you again with annoying error messages. Instead, I’ll list the rewritten function, so you can tweak it and see what happens. Having said that, here’s the pertinent function, this time using a dirty prompt method:

    function showError(){
      try {
          var data=prompt('Enter code for being evaluated');
          eval(data);
      }
      // catch exceptions
      catch(e){
          var p=document.createElement('p');
          p.appendChild(document.createTextNode('An exception was
    thrown by the script. Error name :'+e.name+' Error
    message :'+e.message));
          document.body.appendChild(p);
        }
    }
    // call function when page is loaded
    window.onload=function(){
        var W3C=document.getElementById&&document.
    getElementsByTagName&&document.createElement;
        if(W3C){
            showError();
        }
    }

    As you can see, the function remains nearly the same. The only change worth noting here is the use of a JavaScript prompt() method for entering erroneous (or correct) data and triggering some types of errors. Consider this method as a quick and dirty -- but didactical -- mechanism for implementing an exception handler, if you’re not inclined to test JavaScript code through a web form. In either case, results will closely resemble the ones obtained with forms.

    Wrapping up

    At this point, hopefully you’ve started playing around with JavaScript exceptions, in order to make code that is a lot more robust and efficient. Over this part of the series, I’ve provided you with enough examples to demonstrate how errors can be triggered by using some of the most common data collectors, such as web forms and JavaScript prompts.

    However, even when core concepts and basic examples have been fully covered, a “real world” application of exception handlers is preferred. Bearing this in mind, the next part of the series will be focused entirely on implementing exceptions in an old favorite: form validation. How this is done will be the next topic to be explained over the next tutorial. Don’t miss it!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · The third part of the series demonstrates how to deliberately raise errors, in order...
     

    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 5 Hosted by Hostway
    Stay green...Green IT