js遇到代码出现问题时如何调试代码

单步跟踪调试 debugger;html

控制台watch功能查看变量当前值函数

 

 进入函数操做spa

 

 随着不断点击,不停进行循环,指定变量的值也在发生改变debug

 

 添加断点调试

 

 跳入跳出函数code

 

 throw new Error() 主动抛出异常htm

后面的代码再也不运行blog

代码会跳转到离这句最近的try语句中ip

使用it

try{

}catch(e){

}

接收异常

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        try{
            var foo={};
            console.log(foo.pro);
        }catch(e){
            console.log(e);//undefined
        }finally{
            console.log('异常致使程序停止啦~');//异常致使程序停止啦~
        }
    </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        function multi(num1, num2){
            if(typeof num1 != "number" || typeof num2 != "number"){
                throw new Error('必须输入数字!!!');
            }
            console.log(num1*num2);
        }

        try{
            //multi("a", "b");//Error: 必须输入数字!!!
            multi(1, 2);//2

        }catch(e){
            console.log(e);
        }finally{
            console.log('无论有没有异常我都要执行哈~');
        }
    </script>
</body>
</html>
相关文章
相关标签/搜索