Monday, August 20, 2007

SQL 2005 Try Catch.

In SQL Server 2005 you can use try catch block as in other programming language.Below is the simple example which is generating the divide-by-zero exception and the catch block statement is retrieving the error information.


BEGIN TRY

-- Generate a divide-by-zero error.
SELECT 1/0;

END TRY

BEGIN CATCH

SELECT

ERROR_NUMBER() AS ErrorNumber,

ERROR_SEVERITY() AS ErrorSeverity,

ERROR_STATE() AS ErrorState,

ERROR_PROCEDURE() AS ErrorProcedure,

ERROR_LINE() AS ErrorLine,

ERROR_MESSAGE() AS ErrorMessage;

END CATCH;

No comments: