How do we use throw statement in JavaScript?

JavascriptWeb DevelopmentFront End Technology

With JavaScript, use throw statement to raise your built-in exceptions or your customized exceptions. Later these exceptions can be captured and you can take an appropriate action.

Example

You can try to run the following code to implement throw statement in JavaScript −

<html>
   <head>
      <script>
         <!--
            function myFunc()
               {
                  var a = 100;
                  var b = 0;
                  try {
                     if ( b == 0 ){
                        throw( "Divide by zero error." );
                     } else {
                        var c = a / b;
                     }
                  }
               catch ( e ) {
                  alert("Error: " + e );
               }
            }
         //-->
      </script>
   </head>

   <body>
      <p>Click the following to see the result:</p>
      <form>
         <input type = "button" value = "Click Me" onclick = "myFunc();" />
      </form>
   </body>
</html>
raja
Published on 16-Feb-2018 12:58:20
Advertisements