Home » Interview Q & A » Q12. What is RAISERROR()? Where do you use this?

Q12. What is RAISERROR()? Where do you use this?

Our Categories

Blog Stats

  • 40,773 hits

Calendar

June 2013
M T W T F S S
 12
3456789
10111213141516
17181920212223
24252627282930

Please visit SQLVERSITY.COM for more details.

The name itself RAISERROR is saying that, we are raising an error. Mostly we use RAISERROR 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 RAISERROR 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 about RAISERROR, Click here


Leave a comment