New T-SQL Features in SQL Server 2012
handling is now easier with the introduction of the THROW command in SQL Server
Legacy method (SQL 2005
previous versions, RAISERROR was used to show an error message. RAISERROR
requires a proper message number to be shown when raising any error. The
message number should exist in sys.messages. RAISERROR cannot be used to
re-throw an exception raised in a TRY..CATCH block.
RAISERROR, THROW does not require that an error number to exist in sys.messages
(although it has to be between 50000 and 2147483647). All exceptions being
raised by THROW have a severity of 16.
can throw an error using Throw as below:
50001, ‘Error message’, 1;
will return an error message:
Msg 50001, Level
16, State 1, Line 1 Error message
even allows for re-throwing an exception caught in a TRY..CATCH block, which
RAISERROR was not able to handle:
SELECT ‘Using Throw’
SELECT 1 / 0
above code snippet produces this output:
Msg 8134, Level
Divide by zero
has now made the developer’s life much easier, and developers can now code
independent of the Tester’s input on the exception message.
The current version of 2011 Books Online indicates that RAISERROR has been
deprecated. Due to this, the use of RAISERROR should be minimized.