Skip Navigation LinksHome > Categories > Code from a Category

How to handle SQL SERVER Errors

User Name: codelecturer
Name: Mike Chauhan
Contact Me:
Home Page:
6 years of total IT experience including programming, application development, System Maintenance . 3 years of Experienced and expertise in .Net Framework VB, Visual Basic.Net, ASP.Net 2.0, 3.5 and AD... [More]
Viewed Times: 2718
Add Date: 01/06/2014
In this article we will learn about how to handle sql server errors.
This article covers the basics of TRY CATCH error handling in T-SQL introduced in SQL Server 2005. It includes the usage of common functions to return information about the error and using the TRY CATCH block in stored procedures and transactions.

SQL Server uses the following syntax to capture errors in Transact-SQL statements:
An error occurred

A key difference from SQL Server 2000 is that execution is halted when SQL Server encounters an error. At that point execution transfers to the CATCH block. This error isn't returned to the client application or calling program. The TRY CATCH block consumes the error.

Error Functions Inside the CATCH block there are a number of specialized functions to return information about the error.

These are the functions you can use inside a CATCH block. These functions all return NULL if they are called from outside a CATCH block.

ERROR_NUMBER. The number of the error that occurred. This is similar to @@ERROR except that it will return the same number for the duration of the CATCH block.

ERROR_MESSAGE. The complete text of the error message including any substiture parameters such as object names.

ERROR_LINE. This is the line number of the batch or stored procedure where the error occured.

ERROR_SEVERITY. This is the severity of the error. The CATCH block only fires for errors with severity 11 or higher. Error severities from 11 to 16 are typically user or code errors. Severity levels from 17 to 25 are usually software or hardware errors where processing may not be able to continue.

ERROR_STATE. This is sometimes used by the system to return more information about the error.

ERROR_PROCEDURE. If the error was generated inside a stored procedure this will hold the name of the procedure.

Trapping Errors in Stored Procedures

A TRY CATCH block can catch errors in stored procedures called by other stored procedures. An example is:

Assuming that the ParentError stored procedure calls the ChildError stored procedure which generates an error you'd see output like this:

The TRY CATCH syntax also works well with transactions. Below is a common pattern used inside stored procedures for transactions. Any errors cause the transaction to roll back.

Happy Programming!!!

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.