try catch finally的用法
生活随笔
收集整理的這篇文章主要介紹了
try catch finally的用法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1、不使用try...catch出現(xiàn)異常后,后面的代碼都不會(huì)運(yùn)行了?
login() {console.log(b);console.log('hhhhhhhh');},2、使用了try...catch出現(xiàn)異常后,try...catch后的代碼還會(huì)繼續(xù)運(yùn)行
login() {try{console.log(b);}catch (e){console.log(e);}console.log('hhhhhhhh');},?3、使用了try...catch出現(xiàn)異常后,try...catch里面出現(xiàn)異常的代碼后的代碼不會(huì)繼續(xù)運(yùn)行
login() {try{console.log(b);console.log('222222222222');}catch (e){console.log(e);}console.log('hhhhhhhh');},4、try...catch不能捕獲異步的錯(cuò)誤,異步的錯(cuò)誤通常用promise的catch捕獲
同步代碼都繼續(xù)走了下去,但是報(bào)錯(cuò)
login() {try{setTimeout(() => { console.log(b) // 未聲明b變量}, 1000)console.log('222222222222');}catch (e){console.log(e);}console.log('hhhhhhhh');},5、catch?和?finally語(yǔ)句都是可選的,但在使用?try語(yǔ)句時(shí)必須至少使用一個(gè)
?只能捕獲到同步的異常,不能捕獲語(yǔ)法和異步的異常,在日常使用中需要注意。
?但是語(yǔ)法異常一般開(kāi)發(fā)工具就捕獲了。
?
總結(jié)
以上是生活随笔為你收集整理的try catch finally的用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。