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);