throw语句在JavaScript中起什么作用?

使用 throw语句引发内置异常或自定义异常。以后可以捕获这些异常,您可以采取适当的措施。

示例

您可以尝试运行以下代码来实现throw语句-

<html>
   <head>
      <script>
         <!--
            function myFunc()            {
               var a = 100;
               var b = 0;
               try{
                  if ( b == 0 ){
                     throw( "除以零误差。" );
                  } 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>