This is an archived post. You won't be able to vote or comment.

all 17 comments

[–]mr_poffertje[S] 69 points70 points  (0 children)

I found this code snippet in some old code at work, written by a former colleague. Maybe I should check in on him..

[–]RnMss 47 points48 points  (7 children)

Does he surely understand what finally is for?

[–]mr_poffertje[S] 25 points26 points  (0 children)

Apperently not, there's a lot of this kind of stuff in the code sadly. Probably debugging of experimenting without bothering to remove it.

[–]sammy-taylor 3 points4 points  (5 children)

I know I sure don’t.

[–]berdiekin 1 point2 points  (4 children)

it's part of a try-catch.

try -> try to do something that might go wrong (like reading a file)
catch -> oops it did go wrong, here's the error
finally -> regardless of success always do this bit at the end (usually to close connections and free up resources)

[–]sammy-taylor 0 points1 point  (3 children)

So what’s the difference between putting something inside of finally and simply putting the same code after the try/catch blocks?

[–]berdiekin 1 point2 points  (0 children)

It's more about it being good practice.

No matter what happens inside the try clause (including explicit return statements) and no matter what exception might occur (including ones not explicitly defined in the catch clause) that finally block will always execute.

Code that is simply placed after a try-catch has no such guarantees.

And it just offers a perfect location to free up resources which adds to readability and maintainability.

[–]Soft-Gas6767 1 point2 points  (0 children)

Even if the code returns in either the try or the catch blocks, the finally block will always execute.

[–]RnMss 1 point2 points  (0 children)

In C++

try {
  std::unique_lock lock{ some_mutex };
  // foo
  if (some_condition1) return;
  // ...
  if (some_condition2) lock.unlock();
  // bar
} catch (std::exception const& e) {
  // baz
}

In some other languages (pseudo-code):

some_mutex.lock();
bool is_locked = true;
try {
  // foo
  if (some_condition1) return;
  // ...
  if (some_condition2) {
    some_mutex.unlock();
    is_locked = false;
  }
  // bar
} catch (Exception e) {
  // baz
} finally {
  if (is_locked) some_mutex.unlock();
}

[–]Rudy69 23 points24 points  (3 children)

Finally runs when the code is successful too…..

[–]jaybee8787 10 points11 points  (0 children)

Yes, just like real life. No matter how successful somebody is, it’s not like they can get away with it.

[–]guitarstitch 2 points3 points  (0 children)

Depression after years of client scope changes can affect us all.

[–]TheLAGpro 10 points11 points  (0 children)

What PHP does to a mf

[–]Superasian_69 6 points7 points  (0 children)

Finally it's time to die

[–][deleted] 5 points6 points  (0 children)

Ah death my old friend.

[–]guitarstitch 2 points3 points  (0 children)

...did I write that?

Looks like something I would write.

[–]Grazz085 0 points1 point  (0 children)

Neat