There's no obvious use case for finally in
most client-side JavaScript.
When shouldn't I just use an expression after a
try/catch statement rather than just placing
it in a finally block?
The following were hypotheses of the possible value provided:
Click on the title of any of the following to run the test:
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.
Flanagan, David (2011-04-18). JavaScript: The Definitive Guide: The Definitive Guide (Definitive Guides) (p. 106). OReilly Media - A. Kindle Edition.
This example is a try/catch statement with no finally clause. Although finally is not used as often as catch, it can be useful. However, its behavior requires additional explanation. The finally clause is guaranteed to be executed if any portion of the try block is executed, regardless of how the code in the try block completes. It is generally used to clean up after the code in the try clause.
In the normal case, the JavaScript interpreter reaches the end of the try block and then proceeds to the finally block, which performs any necessary cleanup. If the interpreter left the try block because of a return, continue, or break statement, the finally block is executed before the interpreter jumps to its new destination.
If an exception occurs in the try block and there is an associated catch block to handle the exception, the interpreter first executes the catch block and then the finally block. If there is no local catch block to handle the exception, the interpreter first executes the finally block and then jumps to the nearest containing catch clause.
If a finally block itself causes a jump with a return, continue, break, or throw statement, or by calling a method that throws an exception, the interpreter abandons whatever jump was pending and performs the new jump. For example, if a finally clause throws an exception, that exception replaces any exception that was in the process of being thrown. If a finally clause issues a return statement, the
Flanagan, David (2011-04-18). JavaScript: The Definitive Guide: The Definitive Guide (Definitive Guides) (pp. 107-108). OReilly Media - A. Kindle Edition.
The production TryStatement : try Block Finally is evaluated as follows:
- Let B be the result of evaluating Block.
- Let F be the result of evaluating Finally.
- If F.type is normal, return B.
- Return F.
The production TryStatement : try Block Catch Finally is evaluated as follows:
- Let B be the result of evaluating Block.
- If B.type is throw, then
- Let C be the result of evaluating Catch with parameter B.value.
- Else, B.type is not throw,
- Let C be B.
- Let F be the result of evaluating Finally.
- If F.type is normal, return C.
- Return F.
Standard ECMA-262 ECMAScript Language Specification Edition 5.1 (June 2011)
The
finallyclause contains statements to execute after thetryblock andcatchclause(s) execute but before the statements following thetrystatement. Thefinallyclause executes regardless of whether or not an exception is thrown. If an exception is thrown, the statements in thefinallyclause execute even if nocatchclause handles the exception.You can use the
finallyclause to make your script fail gracefully when an exception occurs; for example, you may need to release a resource that your script has tied up. The following example opens a file and then executes statements that use the file (server-side JavaScript allows you to access files). If an exception is thrown while the file is open, thefinallyclause closes the file before the script fails.MDN, JavaScript Reference
Code that uses function statement has three known interpretations. Some implementations process
Fzeas a Statement, in order. Others, including JScript, evaluateFzeupon entering the execution context that it appears in. Yet others, notably DMDScript and default configuration of BESEN, throw aSyntaxError.For consistent behavior across implementations, do not use function statement; use either
FunctionExpressionorFunctionDeclarationinstead.Example of
FunctionExpression(valid):var Fze; try { Fze = function(b,a){return b.unselectable=a}; /*...*/ } catch(e) { _DumpException(e) }Example of
FunctionDeclaration(valid):// Program code function aa(b,a){return b.unselectable=a}