The THROW Statement in SQL Server 2014
You can use the TRY and CATCH keywords in Transact-SQL code to catch exceptions that occur during execution, such as the violation of a primary key constraint. SQL Server 2012 introduced the THROW statement, which you can use to throw an exception that has been caught in a CATCH block but which cannot be handled gracefully (to log the error), or to throw custom exceptions of your own that consist of an error number, an error message, and a tiny integer state value (between 0 and 255).
The THROW statement includes parameters to define a user-defined error. Alternatively, in a
CATCH block you can use THROW without parameters to re-throw the original error.
This code sample shows how to use the THROW keyword to throw a custom exception.
Using the THROW statement
CREATE PROCEDURE GetCustomerDetails @CustomerKey int
AS
SELECT * FROM Customers WHERE CustomerKey = @CustomerKey
IF @@ROWCOUNT = 0
THROW 50001, ‘Customer does not exist’, 1
The THROW statement includes parameters to define a user-defined error. Alternatively, in a
CATCH block you can use THROW without parameters to re-throw the original error.
This code sample shows how to use the THROW keyword to throw a custom exception.
Using the THROW statement
CREATE PROCEDURE GetCustomerDetails @CustomerKey int
AS
SELECT * FROM Customers WHERE CustomerKey = @CustomerKey
IF @@ROWCOUNT = 0
THROW 50001, ‘Customer does not exist’, 1
The THROW Statement in SQL Server 2014
Reviewed by Unknown
on
12:04 AM
Rating:
No comments: