如何在 JavaScript 中使用自定义异常?
在 JavaScript 中使用 throw 语句,来捕获自定义异常。你可以尝试运行以下代码以处理自定义异常 −
示例
<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>
广告