Friday, October 7, 2011

SQL - Procedural Programming - TRY ... CATCH

The TRY...CATCH construnct was introduced with SQL Server 2005 for error handling using T-SQL. Statements to be tested for an error are enclosed in a BEGIN TRY ... END TRY block.
A CATCH block immediately follows the TRY block, and error-handling logic is stored here. The following examples shows the basic syntax:

BEGIN TRY
-- code that may produce errors
END TRY
BEGIN CATCH
-- error handling logic
END CATCH

SQL Server evaluates each statement in TRY block sequentially. If a runtime error is encountered, control immediately jumps to the CATCH block, and error information can be retrieved, logged, and displayed to the user.

No comments:

Post a Comment