A. Syntax Essentials:
BEGIN TRY
{sql_statement | statement_block}
END TRY
BEGIN CATCH
[{sql_statement | statement_block}]
END CATCH
[ ; ]
Exception section:
within the scope of a catch block, you can use the following system functions to get the error message that caused the catch block to execute:
Error_number () returns the error number.
error_severity () returns the severity.
error_state () returns the error status number.
error_procedure () returns the name of the stored procedure or trigger where the error occurred.
Error_line () returns the line number in the routine that caused the error.
error_message () returns the full text of the error message. The text can include values provided by any replaceable parameter, such as length, object name, or time.
two. Example:
Step 1:
Create PROCEDURE Syslogerror
As
BEGIN
Insert into Systemerrorlog
(Spname,description,logtime)
Values
(Error_procedure (), Convert (nvarchar (MAX), Error_line ()) + ': ' + error_message (), GetDate ())
END
Step 2:
BEGIN TRY
BEGIN TRANSACTION;
....
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF @ @TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION;
END
EXECUTE [dbo]. [Syslogerror];
END CATCH;
Source: http://www.cnblogs.com/Bowen80/archive/2008/07/18/1246124.html
From for notes (Wiz)
SQL Server Try catch error handling