5

Is there any reason to put code in a finally block as opposed to just having code after the try...catch statement. Surely in both cases the code gets run anyway

try {
   something();
} catch (error) {
   error_handling_with(error);
}
// code here gets executed whether in finally clause or not.
finally_something();

Is there any place where finally is essential after try...catch? I can see it has a use in Promises, just not here.

vogomatix
  • 4,856
  • 2
  • 23
  • 46

4 Answers4

6

finally basically runs even if you have an early-return from try-catch or even if you don't handle the error in the try-catch. Here is an example I like:

function myFunction() {
  try {
    console.log('inside "try"');
    return
  } finally {
    console.log('inside "finally"');
  }

  console.log("after try-finally");
}

myFunction()

When you run myFunction(), it will print the following:

inside "try"
inside "finally"

Since you returned from try, it didn't execute any instructions after the try-finally block. But, Javascript did execute the finally block.

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
vighnesh153
  • 4,354
  • 2
  • 13
  • 27
1

I think you have received the correct technical answer for this. However, if you are asking for practical usage, one situation is when you want to close a connection in any case. Here is a pseudo-code to explain:

openConnection();

try {
  DoSomething();
  return;
} catch (anomaly) {
  handleAnomaly();
} finally {
  closeConnection();
}

See in the above case, you close the connection in every case whether there is a handled exception, un-handled exception or happy flow, your connection will be closed and that too without duplication of code.

0

Catch/Finally are optional, the only rule is that 1 of them must always exist.

Finally exists so that code can always be run, disregarding whether or not an error was caught or not.

I would argue that in some cases they are added when they're not needed, but it helps readability, in other cases it might just be a habit.

DragonInTraining
  • 385
  • 1
  • 12
0

Thies are example codes in MDN web docs to explain the important of finally block and it behaviour.

openMyFile();
    try {
      // tie up a resource
      writeMyFile(theData);
    } finally {
      closeMyFile(); // always close the resource
    }

Accordig to the MDN documentation finally execute no matter what the logic fail or not inside the try block.

In most cases inside a function there are code blocks must execute before workflow end.

Ex: no matter what if file is get opened it must close before exit from workflow.

function doIt() {
  try {
    return 1;
  } finally {
    return 2;
  }
}

doIt(); // returns 2

In above code whether it should return 1. It will return 2 because it guarantee the execution of code inside finally block.But according to MDN this is a bad implementation.

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch

YJR
  • 1,099
  • 3
  • 13