THROW - new feature in SQL Server 2012

Bandi
Posted by Bandi under Sql Server category on | Points: 40 | Views : 969
SQL Server 2012 and Latest Version introduced THROW statement to throw the exception in the CATCH block....

BEGIN TRY
SELECT 1/0
END TRY
BEGIN CATCH
THROW
END CATCH


Output:
/*
(0 row(s) affected)
Msg 8134, Level 16, State 1, Line 2
Divide by zero error encountered.

*/

Earlier Version of SQL Server 2012, we have explicitly raise error in the CATCH block to do above logic..

BEGIN TRY
SELECT 1/0
END TRY
BEGIN CATCH
DECLARE @ErrorMessage NVARCHAR(2000), @ErrorSeverity INT
SELECT @ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY()
RAISERROR (@ErrorMessage, @ErrorSeverity, 1)
END CATCH


Output:

(0 row(s) affected)
Msg 50000, Level 16, State 1, Line 8
Divide by zero error encountered.

Comments or Responses

Login to post response