A resource often needs deterministic cleanup even when the work fails. A type implementing AutoCloseable can participate in try-with-resources. Declare the owned resource in the try header and Java calls close when control leaves the block. Normal completion, an exception and an early return all take that cleanup path.
Multiple resources close in reverse declaration order. If a body exception and a close exception both occur, Java keeps the body exception as primary and records the cleanup failure as suppressed. That detail matters in production diagnostics, though these exercises keep close itself successful so the log stays focused on ownership. The browser has no filesystem access here. Instead, small in-memory resources append observable close events; they teach the language behavior without claiming to read a real file or open a network connection.