Sunday, January 6, 2013

THROW (SQL 2012 Error Handling)

THROW (SQL 2012 Error Handling)

TRY - CATCH error testing was added back in SQL 2005.
THROW has finally arrived in SQL 2012
It should be used in place of RAISERROR

Logging errors the old way -
RAISERROR ('An error occurred querying the table.', 16, 1);

And the new way -
THROW 50000, 'An error occurred querying the table.', 1;

Disadvantages to using THROW
  • Error messages are buffered, and don’t appear in real-time (No WITH NOWAIT equivilent)
  • Doesn’t support token substitutions
  • Always uses severity level 16
A full comparison is in  Lenni Lobel's article linked below.
Throwing Errors in SQL Server 2012
THROW (Transact-SQL)

No comments: