JavaScript 中的 throw 语句的作用是什么?
htmljavascriptprogramming scripts更新于 2024/5/31 2:52:17
使用 throw 语句引发内置异常或自定义异常。稍后可以捕获这些异常并采取适当的措施。
示例
您可以尝试运行以下代码来实现 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>点击以下内容查看结果:</p>
<form>
<input type = "button" value = "Click Me" onclick = "myFunc();" />
</form>
</body>
</html>
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

