Tuesday, June 26, 2012

Java Script Tutorial 18 : JavaScript Throw


JavaScript throw statement allows you to create an exception. If you use this statement together with the try .. catch statement, you can control program flow and generate error messages. The exception can be a string, integer, Boolean or an object. Syntax of throw is as:


throw exception


For example


<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var x=prompt("Enter a number between 2 and 5:","");
try

       if(x>5)
         {
             throw "Error 1";
         }
       else if(x<2)
         {
             throw "Error 2";
         }
 }
 catch(err)
 {
       if(err=="Error 1")
         {
              document.write("The value is higher then 5.");
         }
       if(err=="Error 2")
         {
              document.write("The value is lower then 2.");
         }  
 }
</script>
</body>
</html>


No comments:

Post a Comment