欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript中的try catch異常捕獲機(jī)制用法分析

 更新時(shí)間:2016年12月14日 11:07:57   作者:小小小小小亮  
這篇文章主要介紹了javascript中的try catch異常捕獲機(jī)制,簡(jiǎn)單分析了try catch異常捕獲機(jī)制的基本定義與使用方法,需要的朋友可以參考下

本文實(shí)例講述了javascript中的try catch異常捕獲機(jī)制用法。分享給大家供大家參考,具體如下:

1.跟Java一樣,JavaScript也具有try catch塊,進(jìn)行異常捕獲的機(jī)制。

(1)典型的try catch語句

try{
}
catch{
}
finally{
}

跟java中一樣,JS中最為典型的try catch語句也同樣分為了三個(gè)部分,try用于捕獲異常,catch用于處理異常,而finally用于關(guān)閉資源等后續(xù)操作。

舉例:

try{
  throw "error"
}
catch(ex)
{
  console.log(ex);
}
finally{
  console.log("finally")
}

控制臺(tái)依次輸出:error,finally

(2)try,catch塊中,catch塊和finally塊只需要其一即可,因此如下的try catch塊也是可以實(shí)現(xiàn)的

舉例:

try {
  throw "error"
}
finally{
}

只拋出異常,其他情況不一一舉例

(3)try catch塊中包含了try catch塊

try{ 
   try{
       throw "error"
     }
   finally{
      console.log("finally1")
     }
}
catch(ex)
{
   console.log(ex)
}
finally{
  console.log("finally2")
}

如果是像這樣的嵌套循環(huán),那么輸出的順序?yàn)椋篺inally1,error,finally2

(4)嵌套try catch塊中,拋出異常

try{
   try{
      throw "error1"
   }
   catch(ex)
   { 
      console.log(ex);
      throw "error2"
   }
   finally{
     console.log( "finally1")
   }
}
catch(ex)
{
    console.log(ex);
}
finally{
   console.log("finally2")
}

最終的輸出為:error1,finally1,error2,finally2

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript傳值操作技巧總結(jié)》、《javascript編碼操作技巧總結(jié)》、《JavaScript中json操作技巧總結(jié)》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動(dòng)畫特效與技巧匯總》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論