Dina walks ambil()’s house room by room: a return hands her a parcel, a throw sends her to catch with the error’s envelope, and she always passes through…
Why it matters
finally is the place for cleanup that is guaranteed to run (closing a connection, releasing a lock, hiding a spinner), success or error. Misread its order and you get leaked resources, skipped cleanup, or errors that vanish without a trace.
The key idea
finally runs AFTER the return is ready, BEFORE the function leaves
Common mistakes
An error being thrown is swallowed without a trace A return inside finally overrides the return/throw from try and catch, even swallowing an error being thrown. Almost always a bug: don’t return in finally.
Cleanup skipped when try returns or throws first Cleanup placed after the try block, not in finally, is skipped when try or catch leaves first. Put it in finally so it is guaranteed to run.
An error caught but simply gone, no log, no handling An empty catch hides bugs. At least log the error, or throw it on (throw e) if it cannot be handled there.
Try it in this playground
Run ambil(false) to the end Notice: after the return in try, finally runs before the value reaches the caller.
Throw: ambil(true) At the throw, the rest of try is skipped and Dina jumps to catch.
Empty the catch The error is caught and vanishes: the result is undefined, with no log.
Move tutup() after the try block When try or catch returns first, that cleanup never runs.