案例1:
index.html代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script>
function demo(){
try{
alert(str);
}catch(err){
alert(err);
}
}
demo();
</script>
</body>
</html>
案例2:
index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form>
<input id="txt" type="text" />
<input id="btn" type="button" onclick="demo()" value="按钮" />
</form>
<script>
function demo(){
try{
var e = document.getElementById("txt").value;
if(e == ""){
throw "请输入";
}
}catch(err){
alert(err);
}
}
</script>
</body>
</html>