liveBook · Manning

Throw

As explained in 100 SQL Server Mistakes and How to Avoid Them

THROW is a SQL Server statement that rethrows an original exception or raises a specified ad hoc error.

THROW raises a meaningful SQL Server error, either by rethrowing an error already caught or by issuing a specified ad hoc error. It belongs in deliberate error handling rather than as a substitute for cleaning up failed work. section 7.1 section 7.7

A failed statement inside a transaction can leave an open transaction behind if nothing rolls it back. Pair transaction handling with TRY..CATCH so failures are trapped, the transaction can be handled, and an error can be surfaced to the caller. section 7.1 section 7.7

Use THROW as part of a safer failure path:

  • Catch errors with TRY..CATCH rather than allowing failures to pass unmanaged. section 7.7
  • Roll back or otherwise resolve the transaction state before returning from failed transactional work. section 7.1
  • Raise a meaningful error message so an application or support process can identify and act on the failure. section 7.7

How 100 SQL Server Mistakes and How to Avoid Them covers it

How deep it goes
Positions THROW within TRY..CATCH and transaction cleanup, without detailing its syntax or rethrow semantics.
Examples
Uses a renamed sales-order detail table and invalid product IDs to produce transactional database errors.
What it assumes
Introduces transactions as atomic units while assuming basic stored-procedure and T-SQL execution familiarity.
What you can do after
Apply it via T-SQL to raise meaningful errors after handling failures in transactional code.
How it connects
Connects THROW to TRY..CATCH, RAISERROR(), transactions, XACT_ABORT, error severity levels, and application alerting.

Also explained in

Compare how 3 books explain throw →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 SQL Server Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.