javascript異常捕獲筆記

異常捕獲

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();




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章