January 22, 2012

TRY CATCH in SQL Server

We can use TRY CATCH in SQL Server to trap exception in SQL Code block.
So it will be very easy to make exception handling very easy process.

Let’s try to make a simple example to express the concept of exception handling.

T-SQL

BEGIN TRY
DECLARE
@IntVar INT;
SET @IntVar = 60000/0;
END TRY
BEGIN
CATCH
      SELECT 'Divide By Zero Error' AS [Error Message]
END CATCH;
GO

Output:

image 

That’s It.
Enjoy Learning.

No comments:

Post a Comment