Briefle words · Code & architecture
try/finally
A code block whose 'finally' part always runs, whether the code before it succeeded or threw an error.
Say it to an AI
Node + pg reserve(sku, qty) that runs SELECT … FOR UPDATE and the decrement in one transaction on a pooled client. Wrap it in try/catch/finally: ROLLBACK on any error (without letting a failed ROLLBACK mask the original error) and always release the client in finally, passing the error if the connection broke, so an OutOfStock can't leak connections. Throw UnknownSku if no row matches.
How an expert would brief the stronger version of “Stock reservation”.
See it
Try it as a puzzle →Warehouse API function that reserves stock for an order using a pooled Postgres connection and a transaction.
Why: A calls client.release(broken) in a block that runs no matter what. B calls it on the last line, which an OutOfStock error skips.
Related words
Room 4 · Errors & edge cases
Learn “try/finally” in a five-minute lesson