自定义异常:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</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>