topical media & game development

talk show tell print

professional-javascript-14-AssertExample.htm / htm



  <html>
      <head>
          <title>Try Catch Example</title>
          <script type="text/javascript">
                  function assert(bCondition, sErrorMessage) {
                      if (!bCondition) {
                          throw new Error(sErrorMessage);
                      }
                  }
          
                  function addTwoNumbers(a, b) {
                      assert(arguments.length == 2, "Two numbers are required.");
                      return a + b;
                  }
                  
                  try {
                      result = addTwoNumbers(90);
                  } catch (oException) {
                      alert(oException.message);      //outputs "Two numbers are required."
                  }
  
          </script>
      </head>
      <body>
      </body>
  </html>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.