Table of Contents

    tl;dr

    There's no obvious use case for finally in most client-side JavaScript.

    Question

    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:

    Tests

    Click on the title of any of the following to run the test:

    Background

    JDG: The try/catch/finally statement

    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.

    JDG: The try/catch/finally statement

    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.

    ECMA-262: 12.14 The try Statement

    The production TryStatement : try Block Finally is evaluated as follows:

    1. Let B be the result of evaluating Block.
    2. Let F be the result of evaluating Finally.
    3. If F.type is normal, return B.
    4. Return F.

    The production TryStatement : try Block Catch Finally is evaluated as follows:

    1. Let B be the result of evaluating Block.
    2. If B.type is throw, then
    3. Let C be the result of evaluating Catch with parameter B.value.
    4. Else, B.type is not throw,
    5. Let C be B.
    6. Let F be the result of evaluating Finally.
    7. If F.type is normal, return C.
    8. Return F.

    Standard ECMA-262 ECMAScript Language Specification Edition 5.1 (June 2011)

    MDN: The finally clause

    The finally clause contains statements to execute after the try block and catch clause(s) execute but before the statements following the try statement. The finally clause executes regardless of whether or not an exception is thrown. If an exception is thrown, the statements in the finally clause execute even if no catch clause handles the exception.

    You can use the finally clause 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, the finally clause closes the file before the script fails.

    MDN, JavaScript Reference

    CLJS: 4.2 What is a function statement?

    Code that uses function statement has three known interpretations. Some implementations process Fze as a Statement, in order. Others, including JScript, evaluate Fze upon entering the execution context that it appears in. Yet others, notably DMDScript and default configuration of BESEN, throw a SyntaxError.

    For consistent behavior across implementations, do not use function statement; use either FunctionExpression or FunctionDeclaration instead.

    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}
          

    comp.lang.javascript FAQ