zoukankan      html  css  js  c++  java
  • javascript try...catch语句

     try...catch语句将能引发错误的代码放在try块中,并且对应一个相应,然后有异常被抛出。

    语法

    try {
       try_statements
    }
    [catch (exception_var_1 if condition_1) { // non-standard
       catch_statements_1
    }]
    ...
    [catch (exception_var_2) {
       catch_statements_2
    }]
    [finally {
       finally_statements
    }]
    

      

    try_statements
    需要被执行的语句。
    catch_statements_1catch_statements_2
    如果在try块里有异常被抛出时执行的语句。
    exception_var_1exception_var_2
    一个用来保存抛出对象的变量根据相关catch子句。
    condition_1
    一个条件表达式。
    finally_statements
    在try语句块之后执行的语句块.无论是否有异常抛出或捕获这些语句都将执行.

    描述

    try语句包含了由一个或者多个语句组成的try块, 和至少一个catch子句或者一个finally子句的其中一个,或者两个兼有, 下面是三种形式的try声明:

    1. try...catch
      try...finally
      try...catch...finally
      

        

    catch子句中包含特定在try块中抛出异常是执行的语句.也就是,你想试一试这个try语句是否成功,如果不成功你将去控制通过catch块。 如果有在try块中有任何一个语句(或者从try块中调用的函数)抛出异常, 控制立即转向catch子句。 如果在try块中没有异常抛出,这catch子句将会跳过。

    finally子句在try块和catch块之后执行但是在下一个try声明之前执行. 无论是否有异常抛出或捕获它总是执行。

    你可以嵌套一个或者更多的try语句。 如果内部的try语句没有catch子句,那么将会进入包围它的try语句的catch子句。

    你也可以用try语句去处理javascript异常. 参考 JavaScript Guide 了解更多关于Javascript异常的信息。

    无条件的catch子句

    当单个的无条件的catch 子句被使用时, 当有异常抛出时则将会进入catch块执行语句. 例如, 当下面的代码发生异常时,控制将会转向catch子句。

    try {
       throw "myException"; // generates an exception
    }
    catch (e) {
       // statements to handle any exceptions
       logMyErrors(e); // pass exception object to error handler
    }
    

      

     

    条件catch子句

     非标准
    该特性是非标准的,请尽量不要在生产环境中使用它!

    你也可以用一个或者更多条件catch子句来处理特定的异常。在这种情况下,当异常抛出时将会进入合适的catch子句中。 在下面的代码中,try块的代码可能会抛出三种异常: TypeError, RangeError, 和EvalError。当一个异常抛出时,控制将会进入与其对应的catch语句。如果这个异常不是特定的,那么控制将转移到非条件catch子句。

    当用一个非条件catch子句和一个或多个条件语句时,非条件catch子句必须放在最后。否则当到达条件语句之前所有的异常将会被非条件语句拦截。

    提醒:这个功能不符合ECMAscript规范。

    try {
        myroutine(); // may throw three types of exceptions
    } catch (e if e instanceof TypeError) {
        // statements to handle TypeError exceptions
    } catch (e if e instanceof RangeError) {
        // statements to handle RangeError exceptions
    } catch (e if e instanceof EvalError) {
        // statements to handle EvalError exceptions
    } catch (e) {
        // statements to handle any unspecified exceptions
        logMyErrors(e); // pass exception object to error handler
    }
     
    

      

    下面用符合ECMAscript规范的简单的JavaScript来编写相同的“条件catch子句”(显然更加冗长的,但是可以在任何地方运行):

    try {
        myroutine(); // may throw three types of exceptions
    } catch (e) {
        if (e instanceof TypeError) {
            // statements to handle TypeError exceptions
        } else if (e instanceof RangeError) {
            // statements to handle RangeError exceptions
        } else if (e instanceof EvalError) {
            // statements to handle EvalError exceptions
        } else {
           // statements to handle any unspecified exceptions
           logMyErrors(e); // pass exception object to error handler
        }
    }
     
    

      

    异常标识符

    当try块中的抛出一个异常时, exception_var (e.g. the e in catch (e)) 用来保存被抛出声明指定的值。 你可以用这个标识符来获取关于被抛出异常的信息。

    这个标识符是catch子语句内部的。 换言之, 当进入catch子语句时标识符创建,catch子语句执行完毕后,这个表示符将不再可用。

    finally子句

    finally子句在try块和catch块之后执行但是在一个try声明之前执行. 无论是否有异常抛出或捕获它总是执行。 如果有异常抛出finally子句将会被执行,这个声明即使没有catch子句处理异常。

    你可以用finally子句使你的脚本结束更加优雅; 例如,  你可能需要释放你的脚本已经用过的资源。下面的示例打开一个文件然后执行使用文件的语句(服务器端的javaScript允许你访问文件)。如果当文件打开时有一个异常抛出, finally子句关闭文件在脚本结束之前。

    openMyFile()
    try {
       // tie up a resource
       writeMyFile(theData);
    }
    finally {
       closeMyFile(); // always close the resource
    }
     
    

      

    例子

    嵌套 try-blocks

    首先让我们看看这里发生什么:

    try {
      try {
        throw new Error("oops");
      }
      finally {
        console.log("finally");
      }
    }
    catch (ex) {
      console.error("outer", ex.message);
    }
    
    // Output:
    // "finally"
    // "outer" "oops"
    

      

     

    现在,如果我们已经捕获异常通过增加一个catch语句块

    try {
      try {
        throw new Error("oops");
      }
      catch (ex) {
        console.error("inner", ex.message);
      }
      finally {
        console.log("finally");
      }
    }
    catch (ex) {
      console.error("outer", ex.message);
    }
    
    // Output:
    // "inner" "oops"
    // "finally"
    

      

     

    现在,让我们再次抛出错误。

    try {
      try {
        throw new Error("oops");
      }
      catch (ex) {
        console.error("inner", ex.message);
        throw ex;
      }
      finally {
        console.log("finally");
      }
    }
    catch (ex) {
      console.error("outer", ex.message);
    }
    
    // Output:
    // "inner" "oops"
    // "finally"
    // "outer" "oops"
    

      

     

    任何给定的异常只会被离它最近的封闭catch块捕获一次。当然,在“内部”块抛出的任何新异常 (因为catch块里的代码也可以抛出异常),将会被“外部”块所捕获。

    从finally块返回

    如果从finally块中返回一个值,那么这个值将会成为整个try-catch-finally的返回值,无论是否有return语句在try和catch中。这包括在catch块里抛出的异常。

    try {
      try {
        throw new Error("oops");
      }
      catch (ex) {
        console.error("inner", ex.message);
        throw ex;
      }
      finally {
        console.log("finally");
        return;
      }
    }
    catch (ex) {
      console.error("outer", ex.message);
    }
    
    // Output:
    // "inner" "oops"
    // "finally"
     
    

      

    因为finally块里的return语句,外部的“oops”异常没有抛出。从catch块返回的值同样适用。

    规范

    SpecificationStatusComment
    ECMAScript 3rd Edition Standard Initial definition.
    Implemented in JavaScript 1.4
    ECMAScript 5.1 (ECMA-262)
    try statement
    Standard  
    ECMAScript 6 (ECMA-262)
    try statement
    Release Candidate  

    Not part of an ECMA-262 standard: Multiple catch clauses and conditional clauses (SpiderMonkey extension, JavaScript 1.5).

    浏览器兼容性

    FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
    Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
    Conditional clauses
    (non-standard)
    未实现 (Yes) 未实现 未实现 未实现

    参见

  • 相关阅读:
    各种数字证书区别
    微信支付前端对接流程
    ts笔记 流动类型
    支付宝支付前端对接流程
    ts笔记索引签名
    [翻译]Selenium API 命令和操作(JAVA)
    玩一玩yolo目标检测
    快速上手MyBatis
    Swift快速入门
    Windows远程桌面后不能粘贴问题处理
  • 原文地址:https://www.cnblogs.com/hephec/p/4586743.html
Copyright © 2011-2022 走看看