JS错误处理与调试。

try{
    window.someNonexistentFunction();
}catch(error){
    console.log(error.message);
}

错误对象的message属性。

自己在做了一个例子。

try{
    (function(){
        var i = 0;
        console.log(i);
    })();
}catch(error){
    console.log(error.message);
}

当try块中的代码正常执行,就不会执行catch块中的语句。

//函数的原型

try{

  //可能会导致错误的代码

}catch(error){

  //在错误发生时怎么处理

}

猜你喜欢

转载自www.cnblogs.com/Bluelingling/p/9811226.html