SQL Server has an exception model to handle exceptions and errors that occurs in SQL statements. To handle exception in Sql Server we have TRY..CATCH blocks. In Sql Server, against a Try block we can have only one CATCH block.
Ex;
BEGIN TRY
--T-SQL statements
--or T-SQL statement blocks
END TRY
BEGIN CATCH
--T-SQL statements
--or T-SQL statement blocks
END CATCH
This example shows to use try catch block in your sql queries
BEGIN TRY
DECLARE @num INT, @msg varchar(200)
---- Divide by zero to generate Error
SET @num = 5/0
PRINT 'This will not execute'
END TRY
BEGIN CATCH
PRINT 'Error occured that is'
set @msg=(SELECT ERRORMESSAGE())
print @msg;
END CATCH
0 Comment(s)