Home arrow JavaScript arrow Page 4 - JavaScript Errors
JAVASCRIPT

JavaScript Errors


When last we spoke, we discussed the various Events in JavaScript. In this tutorial we will go over some of the errors that can occur and how to deal with them. I know, I know: how can any of your programs possibly fail when you have been taught by the JavaScript Ninja? Well, you can blame that on the good folks that create the various browsers.

Author Info:
By: James Payne
Rating: 4 stars4 stars4 stars4 stars4 stars / 12
January 07, 2008
TABLE OF CONTENTS:
  1. · JavaScript Errors
  2. · Try...Catch
  3. · Throwing the Ball (and Exceptions)
  4. · By the Numbers

print this article
SEARCH DEVARTICLES

JavaScript Errors - By the Numbers
(Page 4 of 4 )

You could do the above with numbers as well. Say you had a virtual lawn. Maybe you are eighty years old and you wear your pants up to your neck. You probably sit on your virtual porch all day people watching. Maybe some of those people get out of line. Here is a crotchety program to handle just such a situation:


<html>

<body>

<script type="text/javascript">

var age=prompt("Enter your age whippersnapper:","")

try

{

if(age>21)

throw "FirstGrump"

else if(age<21)

throw "SecondGrump"

else if(age==21)

throw "FourthGrump"

else if(isNaN(age))

throw "ThirdGrump"

}

catch(er)

{

if(er=="FirstGrump")

alert("I remember when I was your age...<falls asleep> zzzzzz")

if(er == "SecondGrump")

alert("Pull up your pants! And Get off my lawn!")

if(er == "ThirdGrump")

alert("Eh?!? Wassat?!? I didn't here ye")

if(er == "FourthGrump")

alert("Let's go get some booze!")

}

</script>

</body>

</html>



This program asks the young whippersnapper to enter their age. If the age is greater than 21, an exception is thrown and a pop-up appears saying, “I remember when I was your age <falls asleep> zzzzzz” If the age is less than 21, an exception is thrown that says:”Pull up your pants! And get off my lawn!” If the user inputs 21 as their age, a pop up displays exclaiming: “Let's go get some booze!” Finally, if the user inputs any other data, they will get an alert stating: “Eh?!? Wassat?!? I didn't here ye”

That concludes this issue. Next episode we will discuss JavaScript Objects and learn how to use them.

Till then...


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-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials