Thursday, April 29, 2010

Custom Exception in SQL Server

We can use rich functionality of Try Catch block to catch an exception
and we can use ERROR_NUMBER()to get specific errro number.
Custome exception can be thrown by using RAISERROR

Example

BEGIN TRY
exec [dbo].[spSelectRecordsTest]
END TRY

BEGIN CATCH
SELECT
ERROR_NUMBER() as ErrorNumber,
ERROR_MESSAGE() as ErrorMessage;
IF( ERROR_NUMBER()=208)
RAISERROR('INVALLID OBECT NAME ',16,1)
ELSE
RAISERROR('MY CUSTOME EXCEPTION',16,1)
END CATCH;

No comments:

Post a Comment