57

I have a suspicion that I'm using the finally block incorrectly, and that I don't understand the fundamentals of its purpose...

 function myFunc() {
      try {
           if (true) {
                throw "An error";
           }
      } catch (e) {
           alert (e);
           return false;
      } finally {
           return true;
      }
 }

This function will run the catch block, alert "An error", but then return true. Why doesn't it return false?

nickf
  • 537,072
  • 198
  • 649
  • 721

3 Answers3

85

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

The finally block will always run, try returning true after your try block

function myFunc() {
     try {
         if (true) {
               throw "An error";
          }
          return true;
     } catch (e) {
          alert (e);
          return false;
     } finally {
          //do cleanup, etc here
     }
 }
Don Kirkby
  • 53,582
  • 27
  • 205
  • 286
Gilean
  • 14,708
  • 10
  • 45
  • 52
10

Finally blocks execute when you leave the try block. In your code this happens when you return false. That sets the return value to false and attempts to exit the function. But first it has to exit the try block which triggers the finally and overwrites the return value to true.

It is considered by many to be a good programming practice to have a single return statement per function. Consider making a var retval at the beginning of your function and setting it to true or false as appropriate throughout your function and then structuring the code so that it falls correctly through to a single return at the bottom.

Alan Oursland
  • 727
  • 4
  • 7
  • 19
    Using a single return value is one valid and useful strategy for solving the problem of simplifying the intent and clarity of functions. Using a variable to store the return value is also a valid means to solve complicated return value logic. However, I don't think it can be said that multiple return statements in a function are automatically sign of bad programming practice. In many cases it is just fine: simpler in the writing of the code and clear in the conveyance of the function's intent to the next programmer who needs to understand it. `function a(i) {if (i===undefined) {return;} ...}` – ErikE Apr 04 '11 at 19:42
  • 7
    That return statement at the top is abundantly clear. Forcing the entire function to sit inside of an if-block is just not necessary. – ErikE Apr 04 '11 at 19:43
1
function getTheFinallyBlockPoint(someValue) {
    var result;
    try {
        if (someValue === 1) {
            throw new Error("Don't you know that '1' is not an option here?");
        }
        result = someValue
    } catch (e) {
        console.log(e.toString());
        throw e;
    } finally {
        console.log("I'll write this no matter what!!!");
    }

    return result;
};

getTheFinallyBlockPoint("I wrote this only because 'someValue' was not 1!!!");
getTheFinallyBlockPoint(1);

Run this on your browser's console and it might give you the answer you're looking for.

Vemonus
  • 868
  • 1
  • 16
  • 28
Danny Mor
  • 1,143
  • 12
  • 12