javaScript中的异常

来源:互联网 发布:网络安全教育文章 编辑:程序博客网 时间:2024/06/16 07:08

一.javaScript中对异常的处理

<script>

function msg() {
try {
aalert("nihao");
} catch (err) {
txt = "error";
}
}
</script>
</head>
<body>
<input type="button" value="view msg" onclick="msg()">
</body>

</html>

二.throw

<script>
function myf() {
try {
var x = document.getElementById("demo").value;
if (x == "")
throw "empty";
if (isNaN(x))
throw "not a num";
if (x > 10)
throw "big";
if (x < 5)
throw "small";
} catch (err) {
var y = document.getElementById("mess");
y.innerHTML = "error";
}
}
</script>
</head>
<body>
<p>请输入5-10之间的一个数</p>
<input type="text" id="demo">
<button type="button" onclick="myf()">click</button>
<p id="mess"></p>
</body>
</html>

0 0