SQL Server > Error Handling

Handle error in stored procedures in SQL Server

How to handle error in stored procedures in SQL Server?


To handle error in stored procedure of SQL Server, we use BEGING TRY – END TRY and BEGIN CATCH and EDN CATCH statements.

Normally, if we perform any invalid SQL operations, it throws error like in below case

SELECT 'ITFunda'/0

Here, it is not possible divide any string by 0, so SQL Server is trying to convert it to integer however ‘ITFunda’ couldn’t get converted and it throws error.

Instead of raw error coming in, we want to ignore this error, we can wrap the above statements into BEGIN and END TRY like this

BEGIN TRY
       SELECT 'ITFunda'/0
END TRY
BEGIN CATCH
 
END CATCH

Here, the SELECT statement will throw error that is being caught in the BEGIN CATCH block, however this block is not doing anything but suppressing the error. So the output is showing nothing without any error.

 Views: 10821 | Post Order: 97



Write for us






Hosting Recommendations