2

Which programming language first came up with the finally block?

I ask purely out of curiosity.

It is a very useful piece of syntactic sugar, and whoever first created it surely has a very impressive grasp of solutions to programming problems.

(Note: it is deceptively difficult to find an answer to this question...)

patstuart
  • 643
  • 5
  • 12
  • 3
    Why would you call it syntactic sugar? – Pieter B Oct 01 '14 at 18:05
  • 2
    Related reading: [Why is there no 'finally' construct in C++?](http://programmers.stackexchange.com/q/197562/22815) –  Oct 01 '14 at 18:06
  • @PieterB the block helps avoid duplicate code (copy pasting the statement into try and each catch block). A try-finally block helps avoid a try-catch-rethrow block which is ugly, harder to understand, and defeats the purpose of such blocks. To quote [another source](https://bugs.php.net/bug.php?id=32100), everything _can_ be accomplished without the finally block, but everything also _can_ be accomplished on a Turing Machine. That doesn't make it good form, though. – patstuart Oct 01 '14 at 18:13
  • @patstuart Hmm... C is just syntactic sugar for a turing machine? –  Oct 01 '14 at 18:13
  • @MichaelT: I thought C was syntactic sugar for assembly. ;) – FrustratedWithFormsDesigner Oct 01 '14 at 18:27
  • 1
    @PieterB because you can replace try{}finally{...} with `try{}catch(e){...;throw e;}` – ratchet freak Oct 01 '14 at 18:39
  • 5
    @ratchetfreak It's not that simple. `...` is also executed when the `try` block is left via any other means: `continue`, `break`, `return`, and whatever other control flow statements the language offers. Of course there is still a way to replace the `finally` with other constructs but I think it's far beyond the threshold for syntactic sugar. –  Oct 01 '14 at 19:03
  • @ratchetfreak: In the .NET framework, it is possible in VB.NET and probably other languages as well for a Try block to specify code which, if an exception is thrown which is not handled by any inner Try block, should run before inner `finally` methods. Catching and immediately rethrowing an exception is thus not the same thing as not catching it. – supercat Oct 01 '14 at 22:09

1 Answers1

10

According to wikipedia the first to introduce this was NIL.

The cleanup behavior now generally called "finally" was introduced in NIL (New Implementation of LISP) in the mid- to late-1970s as UNWIND-PROTECT.

Pieter B
  • 12,867
  • 1
  • 40
  • 65