标签:
JavaScript 错误 - Throw、Try 和 Catch请注意,如果 getElementById 函数出错,上面的例子也会抛出一个错误。
++++++++++++++++++++++++++++++++++++++++++++++++++++
<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html charset=utf-8"/>
<head>
<script>
var txt="";
function message()
{
try
{
adddlert("Welcome guest!");
}
catch(err)
{
txt="本页有一个错误。\n\n";
txt+="错误描述:" + err.message + "\n\n";
txt+="点击确定继续。\n\n";
alert(txt);
}
}
</script>
</head>
<body>
<input type="button" value="查看消息" onclick="message()" />
</body>
</html>
++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++
<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html charset=utf-8"/>
<body>
<script type="text/javascript">
function myFunction()
{
try
{
var x = document.getElementById("demo").value;
if("") throw "值为空";
if(isNaN(x)) throw "不是数字";
if(x>10) throw "值太大";
if(x<5) throw "值太小";
}
catch(err)
{
var y = document.getElementById("mess");
y.innerHTML = "错误:" + err + "。";
}
}
</script>
<h1>我的第一个 javascript 程序</h1>
<p>请输入 5 到 10 之间的数字:</p>
<input id="demo" type="text">
<button type ="button" onclick = "myFunction()">测试输入值</button>
<p id = "mess"></p>
</body>
</html>
JavaScript--JavaScript 错误 - Throw、Try 和 Catch
标签:
原文地址:http://blog.csdn.net/u012701023/article/details/46315775