topical media & game development

talk show tell print

professional-javascript-14-DebugExample5.htm / htm



  <html>
      <head>
          <title>Debug Example</title>
          <script type="text/javascript">
                  function assert(bCondition, sErrorMessage) {
                      if (!bCondition) {
                          throw new Error(sErrorMessage);
                      }
                  }
  
          
                  function divide(iNum1, iNum2) {
                      assert(arguments.length == 2, "divide() requires two arguments.");
                      assert(typeof iNum1 == "number" && typeof iNum2 == "number", 
                                        "divide() requires two numbers for arguments.");
                  
                      return iNum1.valueOf() / iNum2.valueOf();
                  }
                  
                  alert(divide("a"));
  
          </script>
      </head>
      <body>
          <p>This page uses assertions to throw a custom JavaScript error.</p>
      </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.