User Id :    Password :      New Member   Forgot Password  
 
Error Handling by using JavaScript
Description Error is caught by the catch argument and the proper error message is displayed.   No. of Views     768
  Rating     0
Author Najmul Hoda   Posted On     11 Feb 2010
Tags Java Script    

Sample Code   Download Code

The example below determines the value of a variable called x. If the value of x is higher than 20, lower than 0, or not a number, we are going to throw an error. The error is then caught by the catch argument and the proper error message is displayed:

<script type="text/javascript">
var x=prompt("Enter a number between 0 and 20:","");
try
  {
  if(x>20)
    {
    throw "Err1";
    }
  else if(x<0)
    {
    throw "Err2";
    }
  else if(isNaN(x))
    {
    throw "Err3";
    }
  }
catch(er)
  {
  if(er=="Err1")
    {
    alert("Error! The value is too high");
    }
  if(er=="Err2")
    {
    alert("Error! The value is too low");
    }
  if(er=="Err3")
    {
    alert("Error! The value is not a number");
    }
  }
</script>


About Author

About Author He is a team lead, having 8+ years of experience. He loves writing technical article for .Net developers. Najmul Hoda
No Photo
 
Country United States
Company BirlaSoft
Home Page http://www.magixchat.com

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

   
Write your comment here.
Comment
Verification Code