Introduction
This article will teach us how to implement exception handling in SQL Server. SQL Server provides TRY and CATCH blocks for exception handling.
Exception Handling in SQL Server
An error condition during program execution is called an exception, and the mechanism for resolving such an exception is known as exception handling. We can put all T-SQL statements into a TRY BLOCK, and the code for exception handling can be put into a CATCH block. We can also generate user-defined errors using a THROW block.

Syntax of Exception Handling

BEGIN TRY
/* T-SQL Statements */
END TRY
BEGIN CATCH
- Print Error OR
- Rollback Transaction
END CATCH
In exception handling, all T-SQL statements are put into a try block. If all statements execute without any error, then everything is OK else. Control will go to the catch block.
Types of SQL Server Exceptions
SQL Server contains the following two types of exceptions:
- System Defined
- User Defined
System Defined Exception
In a System Defined Exception, the system generates exceptions (errors).
Example
Declare @val1 int;
Declare @val2 int;
BEGIN TRY
Set @val1=8;
Set @val2=@val1/0; /* Error Occur Here */
END TRY
BEGIN CATCH
Print 'Error Occur that is:'
Print Error_Message()
END CATCH
Output
- Error Occur that is:
- Divide by zero error encountered
User Defined Exception
This type of exception is user-generated, not system generated.
Declare @val1 int;
Declare @val2 int;
BEGIN TRY
Set @val1=8;
Set @val2=@val1%2;
if @val1=1
Print ' Error Not Occur'
else
Begin
Print 'Error Occur';
Throw 60000,'Number Is Even',5
End
END TRY
BEGIN CATCH
Print 'Error Occur that is:'
Print Error_Message()
END CATCH
Output
- Error Occur
- Error Occur that is:
- Number Is Even
Here 60000 denotes the error number, and 5 denotes the state to associate with the message.
The following are system functions and the keyword used within a catch block:
@@ERROR
ERROR_NUMBER()
ERROR_STATE()
ERROR_LINE()
ERROR_MESSAGE()
ERROR_PROCEDURE()
ERROR_SEVERITY()
RAISERROR()
GOTO()
Now we will see some examples to help understand all these functions and keywords.
First, create a table and enter some value into the table as in the following:
Create TABLE Employee
(
Emp_IId Int identity(1,1),
First_Name Nvarchar(MAX) Not NUll,
Last_Name Nvarchar(MAX) Not Null,
Salary Int Not Null check(Salary>20000),
City Nvarchar(Max) Not Null
)
Insert data into Employee.
Select 'Pankaj','Choudhary',25000,'Alwar' Union All
Select 'Rahul','Prajapat',23000,'Alwar' Union All
Select 'Sandeep','Jangid',27000,'Alwar' Union All
Select 'Sanjeev','Baldia',24000,'Alwar' Union All
Select 'Neeraj','Saini',22000,'Alwar' Union All
Select 'Narendra','Sharma',23000,'Alwar' Union All
Select 'Divyanshu','Gupta',25000,'Alwar'
Now execute a select command.
Select * From Employee

Example 1. (@@ERROR)
@@ERROR returns the error number for the last executed T-SQL statements. It returns 0 if the previous Transact-SQL statement encountered no errors; else returns an error number.
Update Employee set Salary=19000 Where Emp_IID=5
IF @@ERROR = 547
PRINT 'A check constraint violation occurred.';
Output
- Msg 547, Level 16, State 0, Line 1
- The UPDATE statement conflicted with the CHECK constraint "CK__Employee__Salary__68487DD7". The conflict occurred in database "Home_Management," table "dbo. Employee", column 'Salary.'
The statement has been terminated.
A check constraint violation occurred.
Example 2. (ERROR_NUMBER)
ERROR_NUMBER() returns the error number that caused the error. It returns zero if called outside the catch block.
BEGIN TRY
Update Employee set Salary=19000 Where Emp_IID=5
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrorNumber;
END CATCH;
GO
Output

Now a question develops of what is diff @@ERROR and ERROR_NUMBER. Let me explain.
- ERROR_NUMBER can only be used in a catch block; outside a catch block, it returns Null, but @@ERROR can be used inside or outside the catch block (see Example 1).
- ERROR_NUMBER is a contrast to @@ERROR, that only returns the error number in the statement immediately after the one that causes an error or the first statement of a CATCH block.
Now we will see an example and observe the differences between them.
BEGIN TRY
Update Employee set Salary=19000 Where Emp_IID=5
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrorNumber;
print @@ERROR
END CATCH;
GO
Output

BEGIN TRY
Update Employee set Salary=19000 Where Emp_IID=5
END TRY
BEGIN CATCH
print @@ERROR
SELECT ERROR_NUMBER() AS ErrorNumber;
END CATCH;
GO
Output









Join the conversation! Your thoughts help the community grow.