本文為大家分析了javascript中try...catch...finally的使用方法,分享給大家供大家參考,具體內(nèi)容如下
稍微復(fù)雜一點(diǎn)點(diǎn),就要用到判斷語(yǔ)句,if else進(jìn)行條件判斷,話說(shuō)if條件else否則,這樣的判斷對(duì)于寫(xiě)程序代碼的碼儂已經(jīng)是非常熟悉不過(guò)了。
如果你覺(jué)得這個(gè)也很簡(jiǎn)單,可能會(huì)用到混合if else條件判斷語(yǔ)句加上try catch 來(lái)處理語(yǔ)句,雖然用try catch能處理任何的對(duì)象,通過(guò)throw扔一條有錯(cuò)誤的語(yǔ)句,接著catch拋出該對(duì)象或者該對(duì)象的錯(cuò)誤,今天我們只說(shuō)try...catch,下面的例子分別拋出數(shù)組、時(shí)間、原型函數(shù)、數(shù)字類(lèi)型等。
function trycatch () { var array = [234], newdate = new Date(), fun = function(){}, is = 12.22, call; try{ throw array + '/n' + newdate.toLocaleString() + ' /n' + fun.prototype.constructor + '/n' + (typeof is == 'number') +' /n' + call ; //小心local后面還有一個(gè)'e' } catch(e){ console.log(e); } finally{ console.log('err finally'); }}trycatch () // 輸出:// 234// 2015/10/12 下午10:07:03 // function (){}// true // undefined 更準(zhǔn)確的說(shuō),try內(nèi)放一條可能產(chǎn)生錯(cuò)誤的語(yǔ)句。當(dāng)try語(yǔ)句開(kāi)始執(zhí)行并拋出錯(cuò)誤時(shí),catch才執(zhí)行內(nèi)部的語(yǔ)句和對(duì)應(yīng)的try內(nèi)的錯(cuò)誤信息message。何時(shí)執(zhí)行finally語(yǔ)句,只有當(dāng)try語(yǔ)句和catch語(yǔ)句執(zhí)行之后,才執(zhí)行finally語(yǔ)句,不論try拋出異?;蛘遚atch捕獲都會(huì)執(zhí)行finally語(yǔ)句。
function trycatch () { try{ throw new Error('koringz'); } catch(e){ console.log(e.message); } finally{ console.log('err finally'); }}trycatch ()// 輸出:// koringz// err finally 通過(guò)try扔出一條錯(cuò)誤的語(yǔ)句,我們看到在catch捕獲到一條錯(cuò)誤的的信息// koringz,但是同樣的finally也輸出了// err finally。雖然我們了解try catch工作流的處理方式,但是并不了解finally塊的代碼處理程序,按照以往我們對(duì)finally語(yǔ)句一貫的思維方式,就是finally輸出不受try和catch的限制和約束。以下是finally的幾個(gè)輸出演示代碼:
function trycatch () { try{ throw new Error('koringz'); } finally{ console.log('err finally'); return console.log('new finally') }}trycatch ()// err finally// new finally 如上所示,try扔一條錯(cuò)誤的語(yǔ)句,finally輸出的結(jié)果是: // err finally // new finally。
function trycatch () { try{ throw new Error('koringz'); } catch(e){ console.log('err finally'); return console.log('new finally') }}trycatch ()// err finally// new finally 如上所示,try扔一條錯(cuò)誤的語(yǔ)句,catch捕獲到錯(cuò)誤輸出結(jié)果同上finally。 // err finally // new finally。
當(dāng)我修改try的語(yǔ)句:
function trycatch () { try{ // } catch(e){ console.log('err finally'); return console.log('new finally') }}trycatch ()// 空(viod)// 空(viod) 結(jié)果就輸出都為空。// 空(viod)。因?yàn)閠ry沒(méi)有扔出錯(cuò)誤,所以catch沒(méi)有捕獲到異常,故輸出結(jié)果就為空。
那么我們?cè)倏纯聪旅孢@個(gè)案例,通過(guò)下面的例子,可能會(huì)讓你更加地了解try catch語(yǔ)句的異常處理。
try{ try{ throw new Error('open'); } catch(e){ console.info(e.message); throw e } finally{ console.log('finally'); }}catch(e){ console.log('op',e.message);}// open// finally// op open 當(dāng)我們?cè)趖ry可能引發(fā)錯(cuò)誤的代碼塊內(nèi)嵌套try catch,通過(guò)嵌套的代碼塊try內(nèi)扔一條可能出現(xiàn)錯(cuò)誤的語(yǔ)句 throw new Error('open');,緊接著嵌套的try將錯(cuò)誤傳遞給嵌套的catch處理,最終通過(guò)嵌套的finally運(yùn)行過(guò)后,我們看到最后一條結(jié)果// op open,其實(shí)嵌套的catch捕獲的錯(cuò)誤信息扔給最外層catch捕獲的。// op open
也就是說(shuō):任何給定的異常只會(huì)被離它最近的封閉catch塊捕獲一次。
當(dāng)然,在“內(nèi)部”塊拋出的任何新異常(因?yàn)閏atch塊里的代碼也可以拋出異常),都將會(huì)被“外部”塊所捕獲。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。















