try/catch/finally
The try/catch/finally statement is JavaScript’s exception handling mechanism.
try/catch/finally语句是js的异常处理机制。
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.
try语句指定代码块,这些代码块的异常需要被处理。当try代码块中的异常发生时,catch代码块被调用。finally块中包含清理代码,不管try块中发生什么,finally代码块一定会执行。
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.