SQL Server TRY...CATCH, THROW and Transactions: A Practical Error-Handling Pattern

A short T-SQL guide explains how to handle errors in stored procedures so failures do not leave data partially updated. It recommends setting XACT_ABORT ON so most run-time errors roll back the whole transaction, including when a client times out and cancels. The pattern wraps the work in TRY...CATCH and rolls back only when XACT_STATE() shows an open transaction. It re-raises the original error with a bare THROW, and uses THROW numbers of 50000 or higher for custom business errors. Compile-time errors and deadlocks are noted as special cases, with retries usually handled by the application.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.


Discussion (0)
Log in to join the discussion and vote.
Log in