Labels

Wednesday, April 3, 2013

Throw

/*
In previous versions, by using TRY..CATCH block we used to catch the exceptions raised by application and by using RAISEERROR(), error has been thrown back to the called application. However RAISEERROR() has few drawbacks.

    It will not allow to throw the same exception number, we got in the code. Instead we need to create a custom exception and needs to to throw it.
    Because we are send the error separately, error line number also will change.

Now in SQL Server 2012, THROW command will solves these problems by allowing to throw the same exception we got in code */


BEGIN TRY
SELECT 1/0
END TRY
BEGIN CATCH
PRINT 'Error Number: ' + CAST(ERROR_NUMBER() AS VARCHAR)
PRINT 'Error Line: ' + CAST(ERROR_LINE() AS VARCHAR)
PRINT 'Throwing error...';
THROW
END CATCH

No comments:

Post a Comment