Exception Handling in SQL SERVER

Exception Handling is a practice to capture the error efficiently and display the error in a well formatted way.

In SQL Server, we use the TRY..CATCH Block for handling exceptions.

The syntax is as follows

BEGIN TRY      
{ sql_statement | statement_block }
END TRY
BEGIN CATCH      
[ { sql_statement | statement_block } ]
END CATCH
[ ; ]

The below shows an exception message for divide by zero error

CREATE PROCEDURE GenerateException
AS
BEGIN
BEGIN TRY
SELECT 1/0
END TRY
BEGIN CATCH
SELECT 'Error Occurred'
END CATCH
END

Comments

Popular posts from this blog

Jesus - God Thinks of you.

ASP.NET 4.0 Feature - URL Routing

Tips on JQuery Intellisense in VS 2008