Overview

BEGIN TRY
  PRINT 10/0;
  PRINT 'No error';
END TRY
BEGIN CATCH
  PRINT 'Error';
END CATCH

Detailed Example

BEGIN TRY
	INSERT INTO HumanResources.Employee(BusinessEntityID, JobTitle) VALUES(1, 'Emp1');
END TRY
BEGIN CATCH
	IF ERROR_NUMBER() = 2627
		PRINT 'Handling PK violation...';
	ELSE IF ERROR_NUMBER() = 547
		PRINT 'Handling CHECK/FK constraint violation...';
	ELSE IF ERROR_NUMBER() = 515
		PRINT 'Handling NULL violation...';
	ELSE IF ERROR_NUMBER() = 245
		PRINT 'Handling conversion error...';
	ELSE PRINT 'Handling unknown error...';

	PRINT 'Error Number  : ' + CAST(ERROR_NUMBER() AS VARCHAR(10));
	PRINT 'Error Message : ' + ERROR_MESSAGE();
	PRINT 'Error Severity: ' + CAST(ERROR_SEVERITY() AS VARCHAR(10));
	PRINT 'Error State   : ' + CAST(ERROR_STATE() AS VARCHAR(10));
	PRINT 'Error Line    : ' + CAST(ERROR_LINE() AS VARCHAR(10));
	PRINT 'Error Proc    : ' + COALESCE(ERROR_PROCEDURE(), 'Not within proc');
END CATCH

THROW

CREATE TABLE dbo.TestRethrow
(    ID INT PRIMARY KEY
);

BEGIN TRY
	INSERT dbo.TestRethrow(ID) VALUES(1);
	--  Force error 2627, Violation of PRIMARY KEY constraint to be raised.
	INSERT dbo.TestRethrow(ID) VALUES(1);
END TRY
BEGIN CATCH
    PRINT 'In catch block.';
    THROW 51000, 'Modified error text.', 1;
END CATCH