異常捕獲
1、異常
當JavaScript引擎執行Javascript代碼時,發生了錯誤,導致程序停止運行
2、異常拋出
當異常產生,並且將這個異常生成一個錯誤信息
3、異常捕獲
try{
發生異常的代碼塊;
}catch(err){
錯誤信息處理;
}
function demo(){ try{ alert(str); }catch(error){ console.log(error);//==>ReferenceError: str is not defined(…) } } demo(); 自定義錯誤: function demo(){ try{ if(str==""){ throw "str爲空"; } }catch(error){ console.log(error);//==>str爲空 } } demo(); |