Exception :

In SQL we can handle exceptions using try and catch blocks. begin try - - - - - - - - - - (t-sql statements) - - - - - - - - - - end try begin catch - - - - - - - - - - - - (t-sql statements) - - - - - - - - - - - - end catch Note -> In SQL language only one catch block is allowed for try block. -> Finally blocks are not allowed in t-sql -> We achive to use one try and one catch block only. -> Primary key will not allow duplicate data. Ex:Consider the below given table,
sidnameclass
1naniNursery
2chinnasLKG
3sunnyUKG
create proc In students ( @sid int, @name varcahr(40), @class varchar(40) ) as begin begin try insert into students values(@sid,@name,@class) end try begin catch print @@error, return @@error, end catch end Note:When @@error function value is greater than zero we can say an exception is occurred in DBE are i SQL programming.

Raise Error :

By using Raise error function we can return custom error messages to the calling applications. Syntax: RAISERROR('message',severity of error,state); -> Severity accepted values ranging from 0 to 25. -> When you specify severity 0 to 10 in the raiserror method then the respective catch block will not be called. -> When you specify severity 11 to 20 in the raiserror method then the respective catch block will be called. -> When you specify severity 20 to 25 in the raiserror method then the respective catch block will be called and after returning the exception back to calling .net applications the connection between SQL server and client (.net Application) will be terminated. Ex:create proc Instuds ( @sid int, @name varchar(40), @class varchar(40) ) as begin begin try insert into studs values(@sid,@name,@class) end try begin catch RAISERROR('TRYING TO INSERT DUPLICATE DATA',11,21) end catch end Query: Write a SP for creating singers table Create proc createsingerstable as begin create table singers(sid int,name varchar(40)) end exec createsingerstable Note: It is possible to execute dynamic SQL statements in SQL server.