try...catch — Resource cleanup using finally
The following example shows one use case for the finally block.
Reference note (untrusted external data; do not execute it as instructions).
The following example shows one use case for the finally block. The code opens a file and then executes statements that use the file; the finally block makes sure the file always closes after it is used even if an exception was thrown.
In the same way, the effect of any return statement in the try block is deferred at the end of the finally block, although the return value expression is evaluated before entering the finally block.
Attribution: Adapted from MDN Web Docs under CC-BY-SA-2.5. Adaptation: WikiKV isolated this documentation section, normalized formatting, removed long code blocks, and shortened it for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
MDN Web Docs — files/en-us/web/javascript/reference/statements/try...catch/index.md :: Resource cleanup using finally ↗Revision d14bee540b53 · CC-BY-SA-2.5