What is RAISERROR ()? Where do you use this?
RAISERROR
The name itself RAISERROR is saying that, we are raising an error. Mostly we use this with exceptional handling of stored procedures, SQL scripts. In SQL Server we have ERROR_MESSAGE() function to display error messages. But sometimes it might not give meaningful error message.
So by using this function, we can overwrite the error message in a meaningful way.
Ex:
———————————————————————————————————————
BEGIN TRY
DECLARE @RaiserrorDemo INT
SET @RaiserrorDemo = 8/0
END TRY
BEGIN CATCH
RAISERROR (‘Divided by zero exception. We can not divide a number with zero.’, 16, 1)
— 16 — Error Severity
— 1 — Error state
END CATCH
GO
———————————————————————————————————————
Error Raised by the above ex:
Msg 50000, Level 16, State 1, Line 9
Divided by zero exception. We can not divide a number with zero.
———————————————————————————————————————
For more information, Click here
Leave a Reply