Mega Code Archive

 
Categories / MSSQL Tutorial / Transact SQL
 

Applying TRY CATCH Error Handling Without Recoding a Stored Procedure

5>  CREATE PROCEDURE usp_SEL_DivideByZero 6> AS 7> SELECT 1/0 8> GO 1> 2> BEGIN TRY 3> EXEC dbo.usp_SEL_DivideByZero 4> END TRY 5> BEGIN CATCH 6> SELECT ERROR_NUMBER() ErrorNBR, ERROR_SEVERITY() Severity, 7> ERROR_LINE () ErrorLine, ERROR_MESSAGE() Msg 8> PRINT 'This stored procedure did not execute properly.' 9> END CATCH 10> GO ----------- (0 rows affected) ErrorNBR    Severity    ErrorLine   Msg ----------- ----------- ----------- ------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------ ----------------------------------------------------------------------------        8134          16           5 Divide by zero error encountered. (1 rows affected) This stored procedure did not execute properly. 1>