Monday, March 5, 2007

Try ..Catch constructs

There are lots of companies,business have already upgraded to SQL Server 2005 ,definetly it makes sense because MS has introduced lots of new featuers. One of the many is how we can handle errors in SQL Server 2005


This procedure can be executed in your CATCH block.

CREATE PROCEDURE ErrorDetails
AS
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;
GO


Connect to Object Explorer and execute the following code

BEGIN TRY
SELECT GETDATE()
SELECT 1/0
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE()
EXEC ErrorDetails
END CATCH;

Great thing that we can catch more errors by usig TRY ...CATCH block and what's more important to get an error message out.

1 comment:

aiya said...
This comment has been removed by a blog administrator.