Js异常捕获

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <script>
            function demo(){
                try{
                    alert(str);
                }catch(e){
                    alert(e);
                }
            }
            demo();
        </script>
        
        <form>
            <input id="txt" type="text"/>
            <input id="btn" type="button" onclick="demo1()" value="按钮" />
        </form>
        <script>
            function demo1(){
                try{
                    var e=document.getElementById("txt").value;
                if(e==""){
                    throw "请输入";
                }
                }catch(e){
                    alert(e);
                }
                
            }
        </script>
    </body>
</html>

 

posted @ 2018-07-12 22:57  一纸年华  阅读(215)  评论(1编辑  收藏  举报