New in SQL Server 2012 as part of exception handling in T-SQL is THROW
Exception handling in previous versions of SQL Server
THROW will raise an exception inside of the catch block and there are already ways to do this in previous versions of SQL Server but where this is an improvement over previous methods is that it simplifies writing the code to raise them.
So lets compare the old and the new.
Here is the T-SQL:
SQL Server 2005/SQL2008
[sourcecode language=’sql’]BEGIN TRY
DECLARE @VarToTest INT
SET @VarToTest = ‘C’
END TRY
BEGIN CATCH
DECLARE
@ErrorMessage nvarchar(4000)
, @ErrorSeverity int
SET @ErrorMessage = ERROR_MESSAGE()
SET @ErrorSeverity = ERROR_SEVERITY()
RAISERROR (@ErrorMessage, @ErrorSeverity, 1 )
END CATCH[/sourcecode]
SQL Server 2012
[sourcecode language=’sql’]BEGIN TRY
DECLARE @VarToTest INT
SET @VarToTest = ‘C’
END TRY
BEGIN CATCH
THROW
END CATCH[/sourcecode]
Msg 245, Level 16, State 1, Line 4 Conversion failed when converting the varchar value ‘C’ to data type int.
For more information see this link from books online – SQL Server 2012 THROW
Leave a Reply