THROW is a modern error-handling statement introduced in SQL Server 2012. It is used to raise custom errors and re-throw original errors inside TRY…CATCH. It also preserves error number, message, severity, and line number. THROW is a modern SQL Server error-handling statement that preserves original error details, is ideal for TRY–CATCH blocks, and should replace RAISERROR in production systems.
Syntex
THROW error_number, message, state;
Here
Ø error_number must be ≥ 50000
Ø message → NVARCHAR(2048)
Ø state → 1 to 255
Ø Severity is always 16
See the example
|
DECLARE
@Salary INT = -5000; THROW 50002, 'Salary cannot be negative',
1; |
See the demo here we Capture full error details in a
log table and Re-throw the original error so application gets correct
error and monitoring tools work to debugging is accurate.
For the demo we are creating a Log table and also creating a
SP where we log the error during the SP execution failed.
|
CREATE TABLE dbo.errorlog |
Now creating SP
|
CREATE PROC dbo.transferamount ( ) BEGIN try BEGIN catch catch END; |
|
EXEC
dbo.TransferAmount @FromAccount = 1, @ToAccount = 2, @Amount = -500; |
Now we are seeing the log table.
No comments:
Post a Comment
If you have any doubt, please let me know.