Using try catch in SQL Server stored procedures

By admin | July 27, 2016 | No Comments

A great new option that was added in SQL Server 2005 was the ability to use the Try..Catch paradigm that exists in other development languages. Doing error handling in SQL Server has not always been the easiest thing, so this option definitely makes it much easier to code for and handle errors.

If you are not familiar with the Try…Catch paradigm it is basically two blocks of code with your stored procedures that lets you execute some code, this is the Try section and if there are errors they are handled in the Catch section.

Let’s take a look at an example of how this can be done. As you can see we are using a basic SELECT statement that is contained within the TRY section, but for some reason if this fails it will run the code in the CATCH section and return the error information.

Source :

Show Buttons
Hide Buttons