zoukankan      html  css  js  c++  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 + '
    ' + newdate.toLocaleString() + ' 
    ' + fun.prototype.constructor + 
            '
    ' + (typeof is == 'number') +' 
    ' + 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

  • 相关阅读:
    汇编中的字符串操作指令
    Scoket需要注意的地方
    判断是否为json对象
    offsetTop,offsetWidth,offsetParent
    ASP.net中页面事件的先后顺序
    opengl32.lib、glu32.lib、 glaux.lib、OpenGL32.lib的意思。
    递归中,方法中的变量值被改变的问题。
    中国数字认证网
    JSON中for in的使用
    (网上转载)JavaScript 跑马灯
  • 原文地址:https://www.cnblogs.com/hao5599/p/4873305.html
Copyright © 2011-2022 走看看