Home arrow JavaScript arrow Page 4 - Exception Handling in JavaScript: Catching User Input
JAVASCRIPT

Exception Handling in JavaScript: Catching User Input


In a previous article, Alejandro Gervasio explained some key concepts related to working with primary error types, and how they are raised by JavaScript interpreters. This article will focus on developing specific sample code for trapping most of the primary error types according to data entered by users.

Author Info:
By: Alejandro Gervasio
Rating: 4 stars4 stars4 stars4 stars4 stars / 14
September 27, 2005
TABLE OF CONTENTS:
  1. · Exception Handling in JavaScript: Catching User Input
  2. · Using multiple error handlers: a quick overview of the previous script
  3. · Catching user data: triggering errors through web forms
  4. · A quick and dirty error handler: using JavaScript prompts

print this article
SEARCH DEVARTICLES

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.

blog comments powered by Disqus
JAVASCRIPT ARTICLES

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials