在js中是否有象try   catch这样的异常捕捉机制

解决方案 »

  1.   


    try
    {}
    catch(e)
    {}
    finally
    {}
      

  2.   

    6.17. try/catch/finally
    The TRy/catch/finally statement is JavaScript's exception-handling mechanism. The TRy clause of this statement simply defines the block of code whose exceptions are to be handled. The try block is followed by a catch clause, which is a block of statements that are invoked when an exception occurs anywhere within the TRy block. The catch clause is followed by a finally block containing cleanup code that is guaranteed to be executed, regardless of what happens in the try block. Both the catch and finally blocks are optional, but a try block must be accompanied by at least one of these blocks. The TRy, catch, and finally blocks all begin and end with curly braces. These braces are a required part of the syntax and cannot be omitted, even if a clause contains only a single statement. Like the tHRow statement, the try/catch/finally statement is standardized by ECMAScript v3 and implemented in JavaScript 1.4.The following code illustrates the syntax and purpose of the TRy/catch/finally statement. In particular, note that the catch keyword is followed by an identifier in parentheses. This identifier is like a function argument. It names a local variable that exists only within the body of the catch block. JavaScript assigns whatever exception object or value was thrown to this variable:try {
      // Normally, this code runs from the top of the block to the bottom
      // without problems. But it can sometimes throw an exception,
      // either directly, with a throw statement, or indirectly, by calling
      // a method that throws an exception.
    }
    catch (e) {
      // The statements in this block are executed if, and only if, the try
      // block throws an exception. These statements can use the local variable
      // e to refer to the Error object or other value that was thrown.
      // This block may handle the exception somehow, may ignore the
      // exception by doing nothing, or may rethrow the exception with throw.
    }
    finally {
      // This block contains statements that are always executed, regardless of
      // what happens in the try block. They are executed whether the try
      // block terminates:
      //   1) normally, after reaching the bottom of the block
      //   2) because of a break, continue, or return statement
      //   3) with an exception that is handled by a catch clause above
      //   4) with an uncaught exception that is still propagating