自定义异常:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Insert title here</title> 6 7 </head> 8 <body> 9 <form> 10 <input id="txt" type="text"> 11 <input id="btn" type="button" onclick="demo()" value="按钮"> 12 </form> 13 <script> 14 function demo(){ 15 try{ 16 var e = document.getElementById("txt").value; 17 if(e==""){ 18 throw "请输入"; 19 } 20 }catch(err){ 21 alert(err); 22 } 23 } 24 </script> 25 26 </body> 27 </html>