Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

How to use Custom Exceptions in JavaScript?



Use throw statement in JavaScript, to catch custom exceptions. You can try to run the following to work with custom exceptions −

Example

<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>
Updated on: 2020-06-23T06:35:16+05:30

197 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements