星空网 > 软件开发 > Java

javascript:理解try...catch...finally

以前,我一直喜欢用console.log(do some thing)去执行输出的类型和值,想马上看到弹出的信息,就会直接在浏览器alert()一下,这些是基础知识。

稍微复杂一点点,就要用到判断语句,if else进行条件判断,话说if条件else否则,这样的判断对于写程序代码的码侬已经是非常熟悉不过了。

如果你觉得这个也很简单,可能会用到混合if else条件判断语句加上try catch 来处理语句,虽然用try catch能处理任何的对象,通过throw扔一条有错误的语句,接着catch抛出该对象或者该对象的错误,今天我们只说try...catch,下面的例子分别抛出数组、时间、原型函数、数字类型等。

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后面还有一个'e'  }  catch(e){    console.log(e);  }  finally{    console.log('err finally');  }}trycatch () 
// 输出:// 234// 2015/10/12 下午10:07:03 // function (){}// true // undefined

更准确的说,try内放一条可能产生错误的语句。当try语句开始执行并抛出错误时,catch才执行内部的语句和对应的try内的错误信息message。何时执行finally语句,只有当try语句和catch语句执行之后,才执行finally语句,不论try抛出异常或者catch捕获都会执行finally语句。

function trycatch () {  try{    throw new Error('koringz');  }  catch(e){    console.log(e.message);  }  finally{    console.log('err finally');  }}trycatch ()// 输出:// koringz// err finally

通过try扔出一条错误的语句,我们看到在catch捕获到一条错误的的信息// koringz,但是同样的finally也输出了// err finally。虽然我们了解try catch工作流的处理方式,但是并不了解finally块的代码处理程序,按照以往我们对finally语句一贯的思维方式,就是finally输出不受try和catch的限制和约束。以下是finally的几个输出演示代码:

function trycatch () {  try{    throw new Error('koringz');  }  finally{    console.log('err finally');    return console.log('new finally')  }}trycatch ()// err finally// new finally

如上所示,try扔一条错误的语句,finally输出的结果是: // 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扔一条错误的语句,catch捕获到错误输出结果同上finally。 // err finally  // new finally。

当我修改try的语句:

function trycatch () {  try{    //   }  catch(e){    console.log('err finally');    return console.log('new finally')  }}trycatch ()// 空(viod)// 空(viod)

结果就输出都为空。// 空(viod)。因为try没有扔出错误,所以catch没有捕获到异常,故输出结果就为空。

那么我们再看看下面这个案例,通过下面的例子,可能会让你更加地了解try catch语句的异常处理。

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

当我们在try可能引发错误的代码块内嵌套try catch,通过嵌套的代码块try内扔一条可能出现错误的语句 throw new Error('open');,紧接着嵌套的try将错误传递给嵌套的catch处理,最终通过嵌套的finally运行过后,我们看到最后一条结果// op open,其实嵌套的catch捕获的错误信息扔给最外层catch捕获的。// op open

也就是说:任何给定的异常只会被离它最近的封闭catch块捕获一次。

当然,在“内部”块抛出的任何新异常(因为catch块里的代码也可以抛出异常),都将会被“外部”块所捕获。

 

 

参考资料:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/try...catch

参考资料:https://msdn.microsoft.com/library/4yahc5d8(v=vs.94).aspx




原标题:javascript:理解try...catch...finally

关键词:JavaScript

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

外媒:融资超1亿美元的亚马逊收购公司已超过25家:https://www.kjdsnews.com/a/556698.html
Airwallex空中云汇:数字经济助推跨境贸易:https://www.kjdsnews.com/a/556699.html
亚马逊测评培训!教你避坑!:https://www.kjdsnews.com/a/556700.html
如何成功打造类目垄断-躺着赚钱:https://www.kjdsnews.com/a/556701.html
卖家必看: 墨西哥电商僵局破解之术!:https://www.kjdsnews.com/a/556702.html
3个月单量激增200倍!小发饰类目成就巨头商家:https://www.kjdsnews.com/a/556703.html
无锡旅游景点竹海 - 无锡的竹海:https://www.vstour.cn/a/363178.html
5月贾汪好玩的地方 贾汪哪有好玩的地方:https://www.vstour.cn/a/363179.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流