1. 程式人生 > >JS錯誤處理與除錯。

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){

  //在錯誤發生時怎麼處理

}