throw : throw : Statement JAVASCRIPT TUTORIALS


JAVASCRIPT TUTORIALS » Statement » throw »

 

throw









Syntax








throw exception



It is used to generate an error condition handled by a try..catch statement or to pass errors found in these statements to higher level handlers.












<html>
    <head>
      <title>Using throw in a try..catch statement</title>
    <script language="JavaScript">
    <!--
    function myErrorHandler(data){
      try{
        if(data == "string"){
          throw "E0";
        }else{
          throw "E1";
        }
      }catch(e) {
        if(e == "E0"){
          return("Error (" + e + "): Entry must be numeric.");
        }else{
          return("Error (" + e + "): Entry must be numeric.");
        }
      }
    }
    function processData(form){
      if(isNaN(parseInt(form.myText.value))){
        alert(myErrorHandler("string"));
      }else{
        alert("You have correctly entered a number");
      }
    }
    -->
    </script>
    </head>
    <body>
    <form name="myForm">
      Please enter a number:
      <input type=TEXT size=10 value="" name="myText">
      <input type=BUTTON value="Process" name="myButton" onClick='processData(this.form)'>
    </form>
    </body>
    </html>







HTML code for linking to this page:

Follow Navioo On Twitter

JAVASCRIPT TUTORIALS

 Navioo Statement
» throw