New Issue: Should our 'try'/'catch' support 'finally'?

28104, "DanilaFe", "Should our 'try'/'catch' support 'finally'?", "2025-11-25T19:48:03Z"

Many other languages I know of that have try/catch also have a third keyword for the concept: finally.

c.f. JS Java C# Ruby Python.

I was very surprised to find that we don't have an equivalent concept, even though (I assume) we took error handling language design from one of these languages. Should Chapel have a finally clause?

proc foo(x: bool) {
  try {
    if x then throw new Error("nooo!");
    writeln("didn't throw");
  } catch e {
    writeln("threw");
  } finally {
    writeln("cleaning up");
  }
}
foo(true);
foo(false);

The above program would print:

threw
cleaning up
didn't throw
cleaning up

Notably, this is not the same as defer, because the defer is executed before the catch handler:

proc foo(x: bool) {
  try {
    defer { writeln("cleaning up"); }
    if x then throw new Error("nooo!");
    writeln("didn't throw");
  } catch e {
    writeln("threw");
  }
}
foo(true);
foo(false);

Prints:

cleaning up
threw
didn't throw
cleaning up