We use try catch blocks just like C#. In this example we will try to divide a number by Zero and let us see how the error message is displayed. When the error comes it goes to catch block.
Write the below script in SQL Server.
- BEGIN TRY
- DECLARE @number INT
- SET @number = 2/0
- PRINT 'This will not execute'
- END TRY
- BEGIN CATCH
- SELECT ERROR_NUMBER() AS ErrorNumber,
- ERROR_SEVERITY() AS ErrorSeverity,
- ERROR_STATE() AS ErrorState,
- ERROR_MESSAGE() AS ErrorMessage;
- END CATCH;
- GO
When we run this script we get the following output.


NitinPosted Mar 10, 2015, 3:28 AM
Thanks Rini
RiniPosted Mar 9, 2015, 3:31 AM
Nice
NitinPosted Feb 26, 2015, 7:16 AM
Thanks Hussain Ahmed
NitinPosted Feb 26, 2015, 7:16 AM
Thanks Rahul Saxena
Hussain AhmedPosted Feb 26, 2015, 5:47 AM
Nice
Rahul Kumar SaxenaPosted Feb 26, 2015, 5:22 AM
Gud One bro..